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 QAction *ascendingAction = itemsMenu->addAction(tr("Sort in &Ascending Order"));
20 QAction *descendingAction = itemsMenu->addAction(tr("Sort in &Descending Order"));
21
22 menuBar()->addMenu(fileMenu);
23 menuBar()->addMenu(itemsMenu);
24
25/* For convenient quoting:
27 QListWidget *listWidget = new QListWidget(this);
29*/
30 listWidget = new QListWidget(this);
32
33 connect(quitAction, &QAction::triggered, this, &QWidget::close);
34 connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
35 connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
36 connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
37 connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
40
41 setupListItems();
42 updateMenus(listWidget->currentItem());
43
44 setCentralWidget(listWidget);
45 setWindowTitle(tr("List Widget"));
46}
47
48void MainWindow::setupListItems()
49{
51 new QListWidgetItem(tr("Oak"), listWidget);
52 new QListWidgetItem(tr("Fir"), listWidget);
53 new QListWidgetItem(tr("Pine"), listWidget);
55 new QListWidgetItem(tr("Birch"), listWidget);
57 new QListWidgetItem(tr("Hazel"), listWidget);
59 new QListWidgetItem(tr("Redwood"), listWidget);
61 new QListWidgetItem(tr("Sycamore"), listWidget);
62 new QListWidgetItem(tr("Chestnut"), listWidget);
63 new QListWidgetItem(tr("Mahogany"), listWidget);
65}
66
67void MainWindow::sortAscending()
68{
70 listWidget->sortItems(Qt::AscendingOrder);
72}
73
74void MainWindow::sortDescending()
75{
77 listWidget->sortItems(Qt::DescendingOrder);
79}
80
81void MainWindow::insertItem()
82{
83 if (!listWidget->currentItem())
84 return;
85
86 QString itemText = QInputDialog::getText(this, tr("Insert Item"),
87 tr("Input text for the new item:"));
88
89 if (itemText.isNull())
90 return;
91
93 QListWidgetItem *newItem = new QListWidgetItem;
94 newItem->setText(itemText);
96 int row = listWidget->row(listWidget->currentItem());
98 listWidget->insertItem(row, newItem);
100
101 QString toolTipText = tr("Tooltip:") + itemText;
102 QString statusTipText = tr("Status tip:") + itemText;
103 QString whatsThisText = tr("What's This?:") + itemText;
105 newItem->setToolTip(toolTipText);
106 newItem->setStatusTip(toolTipText);
107 newItem->setWhatsThis(whatsThisText);
109}
110
111void MainWindow::removeItem()
112{
113 listWidget->takeItem(listWidget->row(listWidget->currentItem()));
114}
115
117{
118 insertAction->setEnabled(current != 0);
119 removeAction->setEnabled(current != 0);
120}
void updateMenus()
void setSelectionMode(QAbstractItemView::SelectionMode mode)
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.
The QListWidgetItem class provides an item for use with the QListWidget item view class.
Definition qlistwidget.h:23
void setText(const QString &text)
Sets the text for the list widget item's to the given text.
The QListWidget class provides an item-based list widget.
QListWidgetItem * takeItem(int row)
Removes and returns the item from the given row in the list widget; otherwise returns \nullptr.
void insertItem(int row, QListWidgetItem *item)
Inserts the item at the position in the list given by row.
void sortItems(Qt::SortOrder order=Qt::AscendingOrder)
Sorts all the items in the list widget according to the specified order.
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
This signal is emitted whenever the current item changes.
QListWidgetItem * currentItem() const
Returns the current item.
int row(const QListWidgetItem *item) const
Returns the row containing the given item.
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
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
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:994
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
@ DescendingOrder
Definition qnamespace.h:123
@ AscendingOrder
Definition qnamespace.h:122
GLenum GLenum GLsizei void * row
#define tr(X)
QMenuBar * menuBar
[0]