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 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:
26//! [0]
27 QListWidget *listWidget = new QListWidget(this);
28//! [0]
29*/
30 listWidget = new QListWidget(this);
31 listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
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);
38 connect(listWidget, &QListWidget::currentItemChanged,
39 this, &MainWindow::updateMenus);
40
41 setupListItems();
42 updateMenus(listWidget->currentItem());
43
44 setCentralWidget(listWidget);
45 setWindowTitle(tr("List Widget"));
46}
47
48void MainWindow::setupListItems()
49{
50//! [1]
51 new QListWidgetItem(tr("Oak"), listWidget);
52 new QListWidgetItem(tr("Fir"), listWidget);
53 new QListWidgetItem(tr("Pine"), listWidget);
54//! [1]
55 new QListWidgetItem(tr("Birch"), listWidget);
56//! [2]
57 new QListWidgetItem(tr("Hazel"), listWidget);
58//! [2]
59 new QListWidgetItem(tr("Redwood"), listWidget);
60//! [3]
61 new QListWidgetItem(tr("Sycamore"), listWidget);
62 new QListWidgetItem(tr("Chestnut"), listWidget);
63 new QListWidgetItem(tr("Mahogany"), listWidget);
64//! [3]
65}
66
67void MainWindow::sortAscending()
68{
69//! [4]
70 listWidget->sortItems(Qt::AscendingOrder);
71//! [4]
72}
73
74void MainWindow::sortDescending()
75{
76//! [5]
77 listWidget->sortItems(Qt::DescendingOrder);
78//! [5]
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
92//! [6]
93 QListWidgetItem *newItem = new QListWidgetItem;
94 newItem->setText(itemText);
95//! [6]
96 int row = listWidget->row(listWidget->currentItem());
97//! [7]
98 listWidget->insertItem(row, newItem);
99//! [7]
100
101 QString toolTipText = tr("Tooltip:") + itemText;
102 QString statusTipText = tr("Status tip:") + itemText;
103 QString whatsThisText = tr("What's This?:") + itemText;
104//! [8]
105 newItem->setToolTip(toolTipText);
106 newItem->setStatusTip(toolTipText);
107 newItem->setWhatsThis(whatsThisText);
108//! [8]
109}
110
111void MainWindow::removeItem()
112{
113 listWidget->takeItem(listWidget->row(listWidget->currentItem()));
114}
115
116void MainWindow::updateMenus(QListWidgetItem *current)
117{
118 insertAction->setEnabled(current != 0);
119 removeAction->setEnabled(current != 0);
120}