|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 //! [quoting modelview_a] |
|
42 #include <QTreeView> |
|
43 #include <QStandardItemModel> |
|
44 #include <QItemSelectionModel> |
|
45 #include "mainwindow.h" |
|
46 |
|
47 MainWindow::MainWindow(QWidget *parent) |
|
48 : QMainWindow(parent) |
|
49 { |
|
50 treeView = new QTreeView(this); |
|
51 setCentralWidget(treeView); |
|
52 standardModel = new QStandardItemModel ; |
|
53 QStandardItem *rootNode = standardModel->invisibleRootItem(); |
|
54 |
|
55 |
|
56 //defining a couple of items |
|
57 QStandardItem *americaItem = new QStandardItem("America"); |
|
58 QStandardItem *mexicoItem = new QStandardItem("Canada"); |
|
59 QStandardItem *usaItem = new QStandardItem("USA"); |
|
60 QStandardItem *bostonItem = new QStandardItem("Boston"); |
|
61 QStandardItem *europeItem = new QStandardItem("Europe"); |
|
62 QStandardItem *italyItem = new QStandardItem("Italy"); |
|
63 QStandardItem *romeItem = new QStandardItem("Rome"); |
|
64 QStandardItem *veronaItem = new QStandardItem("Verona"); |
|
65 |
|
66 //building up the hierarchy |
|
67 rootNode-> appendRow(americaItem); |
|
68 rootNode-> appendRow(europeItem); |
|
69 americaItem-> appendRow(mexicoItem); |
|
70 americaItem-> appendRow(usaItem); |
|
71 usaItem-> appendRow(bostonItem); |
|
72 europeItem-> appendRow(italyItem); |
|
73 italyItem-> appendRow(romeItem); |
|
74 italyItem-> appendRow(veronaItem); |
|
75 |
|
76 //register the model |
|
77 treeView->setModel(standardModel); |
|
78 treeView->expandAll(); |
|
79 |
|
80 //selection changes shall trigger a slot |
|
81 QItemSelectionModel *selectionModel= treeView->selectionModel(); |
|
82 connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)), |
|
83 this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &))); |
|
84 } |
|
85 //! [quoting modelview_a] |
|
86 |
|
87 //------------------------------------------------------------------------------------ |
|
88 |
|
89 //! [quoting modelview_b] |
|
90 void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection & /*oldSelection*/) |
|
91 { |
|
92 //get the text of the selected item |
|
93 const QModelIndex index = treeView->selectionModel()->currentIndex(); |
|
94 QString selectedText = index.data(Qt::DisplayRole).toString(); |
|
95 //find out the hierarchy level of the selected item |
|
96 int hierarchyLevel=1; |
|
97 QModelIndex seekRoot = index; |
|
98 while(seekRoot.parent() != QModelIndex()) |
|
99 { |
|
100 seekRoot = seekRoot.parent(); |
|
101 hierarchyLevel++; |
|
102 } |
|
103 QString showString = QString("%1, Level %2").arg(selectedText) |
|
104 .arg(hierarchyLevel); |
|
105 setWindowTitle(showString); |
|
106 } |
|
107 //! [quoting modelview_b] |
|
108 |
|
109 |