0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the documentation of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include "mainwindow.h"
|
|
45 |
|
|
46 |
MainWindow::MainWindow()
|
|
47 |
{
|
|
48 |
QMenu *fileMenu = new QMenu(tr("&File"));
|
|
49 |
|
|
50 |
QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
|
51 |
quitAction->setShortcut(tr("Ctrl+Q"));
|
|
52 |
|
|
53 |
QMenu *itemsMenu = new QMenu(tr("&Items"));
|
|
54 |
|
|
55 |
insertAction = itemsMenu->addAction(tr("&Insert Item"));
|
|
56 |
removeAction = itemsMenu->addAction(tr("&Remove Item"));
|
|
57 |
QAction *ascendingAction = itemsMenu->addAction(tr("Sort in &Ascending Order"));
|
|
58 |
QAction *descendingAction = itemsMenu->addAction(tr("Sort in &Descending Order"));
|
|
59 |
|
|
60 |
menuBar()->addMenu(fileMenu);
|
|
61 |
menuBar()->addMenu(itemsMenu);
|
|
62 |
|
|
63 |
/* For convenient quoting:
|
|
64 |
//! [0]
|
|
65 |
QListWidget *listWidget = new QListWidget(this);
|
|
66 |
//! [0]
|
|
67 |
*/
|
|
68 |
listWidget = new QListWidget(this);
|
|
69 |
listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
70 |
|
|
71 |
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
|
|
72 |
connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending()));
|
|
73 |
connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending()));
|
|
74 |
connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem()));
|
|
75 |
connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem()));
|
|
76 |
connect(listWidget,
|
|
77 |
SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
|
|
78 |
this, SLOT(updateMenus(QListWidgetItem *)));
|
|
79 |
|
|
80 |
setupListItems();
|
|
81 |
updateMenus(listWidget->currentItem());
|
|
82 |
|
|
83 |
setCentralWidget(listWidget);
|
|
84 |
setWindowTitle(tr("List Widget"));
|
|
85 |
}
|
|
86 |
|
|
87 |
void MainWindow::setupListItems()
|
|
88 |
{
|
|
89 |
//! [1]
|
|
90 |
new QListWidgetItem(tr("Oak"), listWidget);
|
|
91 |
new QListWidgetItem(tr("Fir"), listWidget);
|
|
92 |
new QListWidgetItem(tr("Pine"), listWidget);
|
|
93 |
//! [1]
|
|
94 |
new QListWidgetItem(tr("Birch"), listWidget);
|
|
95 |
//! [2]
|
|
96 |
new QListWidgetItem(tr("Hazel"), listWidget);
|
|
97 |
//! [2]
|
|
98 |
new QListWidgetItem(tr("Redwood"), listWidget);
|
|
99 |
//! [3]
|
|
100 |
new QListWidgetItem(tr("Sycamore"), listWidget);
|
|
101 |
new QListWidgetItem(tr("Chestnut"), listWidget);
|
|
102 |
new QListWidgetItem(tr("Mahogany"), listWidget);
|
|
103 |
//! [3]
|
|
104 |
}
|
|
105 |
|
|
106 |
void MainWindow::sortAscending()
|
|
107 |
{
|
|
108 |
//! [4]
|
|
109 |
listWidget->sortItems(Qt::AscendingOrder);
|
|
110 |
//! [4]
|
|
111 |
}
|
|
112 |
|
|
113 |
void MainWindow::sortDescending()
|
|
114 |
{
|
|
115 |
//! [5]
|
|
116 |
listWidget->sortItems(Qt::DescendingOrder);
|
|
117 |
//! [5]
|
|
118 |
}
|
|
119 |
|
|
120 |
void MainWindow::insertItem()
|
|
121 |
{
|
|
122 |
if (!listWidget->currentItem())
|
|
123 |
return;
|
|
124 |
|
|
125 |
QString itemText = QInputDialog::getText(this, tr("Insert Item"),
|
|
126 |
tr("Input text for the new item:"));
|
|
127 |
|
|
128 |
if (itemText.isNull())
|
|
129 |
return;
|
|
130 |
|
|
131 |
//! [6]
|
|
132 |
QListWidgetItem *newItem = new QListWidgetItem;
|
|
133 |
newItem->setText(itemText);
|
|
134 |
//! [6]
|
|
135 |
int row = listWidget->row(listWidget->currentItem());
|
|
136 |
//! [7]
|
|
137 |
listWidget->insertItem(row, newItem);
|
|
138 |
//! [7]
|
|
139 |
|
|
140 |
QString toolTipText = tr("Tooltip:") + itemText;
|
|
141 |
QString statusTipText = tr("Status tip:") + itemText;
|
|
142 |
QString whatsThisText = tr("What's This?:") + itemText;
|
|
143 |
//! [8]
|
|
144 |
newItem->setToolTip(toolTipText);
|
|
145 |
newItem->setStatusTip(toolTipText);
|
|
146 |
newItem->setWhatsThis(whatsThisText);
|
|
147 |
//! [8]
|
|
148 |
}
|
|
149 |
|
|
150 |
void MainWindow::removeItem()
|
|
151 |
{
|
|
152 |
listWidget->takeItem(listWidget->row(listWidget->currentItem()));
|
|
153 |
}
|
|
154 |
|
|
155 |
void MainWindow::updateMenus(QListWidgetItem *current)
|
|
156 |
{
|
|
157 |
insertAction->setEnabled(current != 0);
|
|
158 |
removeAction->setEnabled(current != 0);
|
|
159 |
}
|