Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mainwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5
6#include "mainwindow.h"
7
8MainWindow::MainWindow()
9{
10 QMenu *fileMenu = new QMenu(tr("&File"));
11
12 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
13 quitAction->setShortcut(tr("Ctrl+Q"));
14
15 QMenu *itemsMenu = new QMenu(tr("&Items"));
16
17 insertAction = itemsMenu->addAction(tr("&Insert Item"));
18 removeAction = itemsMenu->addAction(tr("&Remove Item"));
19 removeAction->setEnabled(false);
20 itemsMenu->addSeparator();
21 ascendingAction = itemsMenu->addAction(tr("Sort in &Ascending Order"));
22 descendingAction = itemsMenu->addAction(tr("Sort in &Descending Order"));
23 autoSortAction = itemsMenu->addAction(tr("&Automatically Sort Items"));
24 autoSortAction->setCheckable(true);
25 itemsMenu->addSeparator();
26 QAction *findItemsAction = itemsMenu->addAction(tr("&Find Items"));
27 findItemsAction->setShortcut(tr("Ctrl+F"));
28
29 menuBar()->addMenu(fileMenu);
30 menuBar()->addMenu(itemsMenu);
31
32/* For convenient quoting:
33//! [0]
34 QTreeWidget *treeWidget = new QTreeWidget(this);
35//! [0]
36*/
37 treeWidget = new QTreeWidget(this);
38//! [1]
39 treeWidget->setColumnCount(2);
40//! [1] //! [2]
41 QStringList headers;
42 headers << tr("Subject") << tr("Default");
43 treeWidget->setHeaderLabels(headers);
44//! [2]
45
46 connect(quitAction, &QAction::triggered, this, &QWidget::close);
47 connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
48 connect(autoSortAction, &QAction::triggered, this, &MainWindow::updateSortItems);
49 connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
50 connect(findItemsAction, &QAction::triggered, this, &MainWindow::findItems);
51 connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
52 connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
53 connect(treeWidget, &QTreeWidget::currentItemChanged,
54 this, &MainWindow::updateMenus);
55
56 setupTreeItems();
57 updateMenus(treeWidget->currentItem());
58
59 setCentralWidget(treeWidget);
60 setWindowTitle(tr("Tree Widget"));
61}
62
63void MainWindow::setupTreeItems()
64{
65//! [3]
66 QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
67 cities->setText(0, tr("Cities"));
68 QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
69 osloItem->setText(0, tr("Oslo"));
70 osloItem->setText(1, tr("Yes"));
71//! [3]
72
73 (new QTreeWidgetItem(cities))->setText(0, tr("Stockholm"));
74 (new QTreeWidgetItem(cities))->setText(0, tr("Helsinki"));
75 (new QTreeWidgetItem(cities))->setText(0, tr("Copenhagen"));
76
77//! [4] //! [5]
78 QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget, cities);
79//! [4]
80 planets->setText(0, tr("Planets"));
81//! [5]
82 (new QTreeWidgetItem(planets))->setText(0, tr("Mercury"));
83 (new QTreeWidgetItem(planets))->setText(0, tr("Venus"));
84
85 QTreeWidgetItem *earthItem = new QTreeWidgetItem(planets);
86 earthItem->setText(0, tr("Earth"));
87 earthItem->setText(1, tr("Yes"));
88
89 (new QTreeWidgetItem(planets))->setText(0, tr("Mars"));
90 (new QTreeWidgetItem(planets))->setText(0, tr("Jupiter"));
91 (new QTreeWidgetItem(planets))->setText(0, tr("Saturn"));
92 (new QTreeWidgetItem(planets))->setText(0, tr("Uranus"));
93 (new QTreeWidgetItem(planets))->setText(0, tr("Neptune"));
94 (new QTreeWidgetItem(planets))->setText(0, tr("Pluto"));
95}
96
97void MainWindow::findItems()
98{
99 QString itemText = QInputDialog::getText(this, tr("Find Items"),
100 tr("Text to find (including wildcards):"));
101
102 if (itemText.isEmpty())
103 return;
104
105 const QList<QTreeWidgetItem *> items = treeWidget->selectedItems();
106 for (QTreeWidgetItem *item : items)
107 item->setSelected(false);
108
109//! [7]
110 const QList<QTreeWidgetItem *> found = treeWidget->findItems(
111 itemText, Qt::MatchWildcard);
112
113 for (QTreeWidgetItem *item : found) {
114 item->setSelected(true);
115 // Show the item->text(0) for each item.
116 }
117//! [7]
118}
119
120void MainWindow::insertItem()
121{
122 QTreeWidgetItem *currentItem = treeWidget->currentItem();
123
124 if (!currentItem)
125 return;
126
127 QString itemText = QInputDialog::getText(this, tr("Insert Item"),
128 tr("Input text for the new item:"));
129
130 if (itemText.isEmpty())
131 return;
132
133//! [8]
134 QTreeWidgetItem *parent = currentItem->parent();
135 QTreeWidgetItem *newItem;
136 if (parent)
137 newItem = new QTreeWidgetItem(parent, treeWidget->currentItem());
138 else
139//! [8] //! [9]
140 newItem = new QTreeWidgetItem(treeWidget, treeWidget->currentItem());
141//! [9]
142
143 newItem->setText(0, itemText);
144}
145
146void MainWindow::removeItem()
147{
148 QTreeWidgetItem *currentItem = treeWidget->currentItem();
149
150 if (!currentItem)
151 return;
152
153//! [10]
154 QTreeWidgetItem *parent = currentItem->parent();
155 int index;
156
157 if (parent) {
158 index = parent->indexOfChild(treeWidget->currentItem());
159 delete parent->takeChild(index);
160 } else {
161 index = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
162 delete treeWidget->takeTopLevelItem(index);
163//! [10] //! [11]
164 }
165//! [11]
166}
167
168void MainWindow::sortAscending()
169{
170 treeWidget->sortItems(0, Qt::AscendingOrder);
171}
172
173void MainWindow::sortDescending()
174{
175 treeWidget->sortItems(0, Qt::DescendingOrder);
176}
177
178void MainWindow::updateMenus(QTreeWidgetItem *current)
179{
180 insertAction->setEnabled(current != 0);
181 removeAction->setEnabled(current != 0);
182}
183
184void MainWindow::updateSortItems()
185{
186 ascendingAction->setEnabled(!autoSortAction->isChecked());
187 descendingAction->setEnabled(!autoSortAction->isChecked());
188
189 treeWidget->setSortingEnabled(autoSortAction->isChecked());
190}