Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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 QTreeWidget *treeWidget = new QTreeWidget(this);
34*/
35 treeWidget = new QTreeWidget(this);
37 QStringList headers;
38 headers << tr("Subject") << tr("Default");
40
41 connect(quitAction, &QAction::triggered, this, &QWidget::close);
42 connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
43 connect(autoSortAction, &QAction::triggered, this, &MainWindow::updateSortItems);
44 connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
45 connect(findItemsAction, &QAction::triggered, this, &MainWindow::findItems);
46 connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
47 connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
50
51 setupTreeItems();
53
55 setWindowTitle(tr("Tree Widget"));
56}
57
58void MainWindow::setupTreeItems()
59{
61 planets->setText(0, tr("Planets"));
62 (new QTreeWidgetItem(planets))->setText(0, tr("Mercury"));
63 (new QTreeWidgetItem(planets))->setText(0, tr("Venus"));
64
65 QTreeWidgetItem *earthItem = new QTreeWidgetItem(planets);
66 earthItem->setText(0, tr("Earth"));
67 earthItem->setText(1, tr("Yes"));
68
69 (new QTreeWidgetItem(planets))->setText(0, tr("Mars"));
70 (new QTreeWidgetItem(planets))->setText(0, tr("Jupiter"));
71 (new QTreeWidgetItem(planets))->setText(0, tr("Saturn"));
72 (new QTreeWidgetItem(planets))->setText(0, tr("Uranus"));
73 (new QTreeWidgetItem(planets))->setText(0, tr("Neptune"));
74 (new QTreeWidgetItem(planets))->setText(0, tr("Pluto"));
75}
76
77void MainWindow::findItems()
78{
79 QString itemText = QInputDialog::getText(this, tr("Find Items"),
80 tr("Text to find:"));
81
82 if (itemText.isEmpty())
83 return;
84
87 while (*it) {
88 if ((*it)->text(0) == itemText)
89 (*it)->setSelected(true);
90 ++it;
91 }
93}
94
95void MainWindow::insertItem()
96{
97 QTreeWidgetItem *currentItem = treeWidget->currentItem();
98
99 if (!currentItem)
100 return;
101
102 QString itemText = QInputDialog::getText(this, tr("Insert Item"),
103 tr("Input text for the new item:"));
104
105 if (itemText.isEmpty())
106 return;
107
108 QTreeWidgetItem *parent = currentItem->parent();
109 QTreeWidgetItem *newItem;
110 if (parent)
111 newItem = new QTreeWidgetItem(parent, treeWidget->currentItem());
112 else
114
115 newItem->setText(0, itemText);
116}
117
118void MainWindow::removeItem()
119{
120 QTreeWidgetItem *currentItem = treeWidget->currentItem();
121
122 if (!currentItem)
123 return;
124
125 QTreeWidgetItem *parent = currentItem->parent();
126 int index;
127
128 if (parent) {
129 index = parent->indexOfChild(treeWidget->currentItem());
130 delete parent->takeChild(index);
131 } else {
134 }
135}
136
137void MainWindow::sortAscending()
138{
140}
141
142void MainWindow::sortDescending()
143{
145}
146
148{
149 insertAction->setEnabled(current != 0);
150 removeAction->setEnabled(current != 0);
151}
152
153void MainWindow::updateSortItems()
154{
155 ascendingAction->setEnabled(!autoSortAction->isChecked());
156 descendingAction->setEnabled(!autoSortAction->isChecked());
157
158 treeWidget->setSortingEnabled(autoSortAction->isChecked());
159}
void updateMenus()
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void triggered(bool checked=false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a m...
void setEnabled(bool)
Definition qaction.cpp:927
static QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode echo=QLineEdit::Normal, const QString &text=QString(), bool *ok=nullptr, Qt::WindowFlags flags=Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints=Qt::ImhNone)
Static convenience function to get a string from the user.
void setCentralWidget(QWidget *widget)
Sets the given widget to be the main window's central widget.
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:768
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void setSortingEnabled(bool enable)
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.
Definition qtreewidget.h:23
QTreeWidgetItem * parent() const
Returns the item's parent.
The QTreeWidget class provides a tree view that uses a predefined tree model.
void sortItems(int column, Qt::SortOrder order)
Sorts the items in the widget in the specified order by the values in the given column.
QTreeWidgetItem * takeTopLevelItem(int index)
Removes the top-level item at the given index in the tree and returns it, otherwise returns \nullptr;...
void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
This signal is emitted when the current item changes.
int indexOfTopLevelItem(QTreeWidgetItem *item) const
Returns the index of the given top-level item, or -1 if the item cannot be found.
QTreeWidgetItem * currentItem() const
Returns the current item in the tree widget.
void setColumnCount(int columns)
void setHeaderLabels(const QStringList &labels)
Adds a column in the header for each item in the labels list, and sets the label for each column.
bool close()
Closes this widget.
Definition qwidget.cpp:8562
void insertAction(QAction *before, QAction *action)
Inserts the action action to this widget's list of actions, before the action before.
Definition qwidget.cpp:3142
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
QSet< QString >::iterator it
@ DescendingOrder
Definition qnamespace.h:123
@ AscendingOrder
Definition qnamespace.h:122
GLuint index
[2]
#define tr(X)
QTreeWidget * treeWidget
[0]
insertRed setText("insert red text")
QMenuBar * menuBar
[0]