8MainWindow::MainWindow()
10 QMenu *fileMenu =
new QMenu(tr(
"&File"));
12 QAction *quitAction = fileMenu->addAction(tr(
"E&xit"));
13 quitAction->setShortcut(tr(
"Ctrl+Q"));
15 QMenu *itemsMenu =
new QMenu(tr(
"&Items"));
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"));
29 menuBar()->addMenu(fileMenu);
30 menuBar()->addMenu(itemsMenu);
33
34
35
36
37 treeWidget =
new QTreeWidget(
this);
39 treeWidget->setColumnCount(2);
42 headers << tr(
"Subject") << tr(
"Default");
43 treeWidget->setHeaderLabels(headers);
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);
57 updateMenus(treeWidget->currentItem());
59 setCentralWidget(treeWidget);
60 setWindowTitle(tr(
"Tree Widget"));
63void MainWindow::setupTreeItems()
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"));
73 (
new QTreeWidgetItem(cities))->setText(0, tr(
"Stockholm"));
74 (
new QTreeWidgetItem(cities))->setText(0, tr(
"Helsinki"));
75 (
new QTreeWidgetItem(cities))->setText(0, tr(
"Copenhagen"));
78 QTreeWidgetItem *planets =
new QTreeWidgetItem(treeWidget, cities);
80 planets->setText(0, tr(
"Planets"));
82 (
new QTreeWidgetItem(planets))->setText(0, tr(
"Mercury"));
83 (
new QTreeWidgetItem(planets))->setText(0, tr(
"Venus"));
85 QTreeWidgetItem *earthItem =
new QTreeWidgetItem(planets);
86 earthItem->setText(0, tr(
"Earth"));
87 earthItem->setText(1, tr(
"Yes"));
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"));
97void MainWindow::findItems()
99 QString itemText = QInputDialog::getText(
this, tr(
"Find Items"),
100 tr(
"Text to find (including wildcards):"));
102 if (itemText.isEmpty())
105 const QList<QTreeWidgetItem *> items = treeWidget->selectedItems();
106 for (QTreeWidgetItem *item : items)
107 item->setSelected(
false);
110 const QList<QTreeWidgetItem *> found = treeWidget->findItems(
111 itemText, Qt::MatchWildcard);
113 for (QTreeWidgetItem *item : found) {
114 item->setSelected(
true);
120void MainWindow::insertItem()
122 QTreeWidgetItem *currentItem = treeWidget->currentItem();
127 QString itemText = QInputDialog::getText(
this, tr(
"Insert Item"),
128 tr(
"Input text for the new item:"));
130 if (itemText.isEmpty())
134 QTreeWidgetItem *parent = currentItem->parent();
135 QTreeWidgetItem *newItem;
137 newItem =
new QTreeWidgetItem(parent, treeWidget->currentItem());
140 newItem =
new QTreeWidgetItem(treeWidget, treeWidget->currentItem());
143 newItem->setText(0, itemText);
146void MainWindow::removeItem()
148 QTreeWidgetItem *currentItem = treeWidget->currentItem();
154 QTreeWidgetItem *parent = currentItem->parent();
158 index = parent->indexOfChild(treeWidget->currentItem());
159 delete parent->takeChild(index);
161 index = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
162 delete treeWidget->takeTopLevelItem(index);
168void MainWindow::sortAscending()
170 treeWidget->sortItems(0, Qt::AscendingOrder);
173void MainWindow::sortDescending()
175 treeWidget->sortItems(0, Qt::DescendingOrder);
178void MainWindow::updateMenus(QTreeWidgetItem *current)
180 insertAction->setEnabled(current != 0);
181 removeAction->setEnabled(current != 0);
184void MainWindow::updateSortItems()
186 ascendingAction->setEnabled(!autoSortAction->isChecked());
187 descendingAction->setEnabled(!autoSortAction->isChecked());
189 treeWidget->setSortingEnabled(autoSortAction->isChecked());