1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: mainwindow.cpp Example File (widgets/icons/mainwindow.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><small><small>widgets/icons/mainwindow.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include "iconpreviewarea.h"
51
#include "iconsizespinbox.h"
52
#include "imagedelegate.h"
53
#include "mainwindow.h"
55
MainWindow::MainWindow()
57
centralWidget = new QWidget;
58
setCentralWidget(centralWidget);
60
createPreviewGroupBox();
61
createImagesGroupBox();
62
createIconSizeGroupBox();
68
QGridLayout *mainLayout = new QGridLayout;
69
mainLayout->addWidget(imagesGroupBox, 0, 0);
70
mainLayout->addWidget(iconSizeGroupBox, 1, 0);
71
mainLayout->addWidget(previewGroupBox, 0, 1, 2, 1);
72
centralWidget->setLayout(mainLayout);
74
setWindowTitle(tr("Icons"));
76
otherRadioButton->click();
81
void MainWindow::about()
83
QMessageBox::about(this, tr("About Icons"),
84
tr("The <b>Icons</b> example illustrates how Qt renders an icon in "
85
"different modes (active, normal, and disabled) and states (on "
86
"and off) based on a set of images."));
89
void MainWindow::changeStyle(bool checked)
94
QAction *action = qobject_cast<QAction *>(sender());
95
QStyle *style = QStyleFactory::create(action->data().toString());
97
QApplication::setStyle(style);
99
smallRadioButton->setText(tr("Small (%1 ļæ½ %1)")
100
.arg(style->pixelMetric(QStyle::PM_SmallIconSize)));
101
largeRadioButton->setText(tr("Large (%1 ļæ½ %1)")
102
.arg(style->pixelMetric(QStyle::PM_LargeIconSize)));
103
toolBarRadioButton->setText(tr("Toolbars (%1 ļæ½ %1)")
104
.arg(style->pixelMetric(QStyle::PM_ToolBarIconSize)));
105
listViewRadioButton->setText(tr("List views (%1 ļæ½ %1)")
106
.arg(style->pixelMetric(QStyle::PM_ListViewIconSize)));
107
iconViewRadioButton->setText(tr("Icon views (%1 ļæ½ %1)")
108
.arg(style->pixelMetric(QStyle::PM_IconViewIconSize)));
113
void MainWindow::changeSize()
117
if (otherRadioButton->isChecked()) {
118
extent = otherSpinBox->value();
120
QStyle::PixelMetric metric;
122
if (smallRadioButton->isChecked()) {
123
metric = QStyle::PM_SmallIconSize;
124
} else if (largeRadioButton->isChecked()) {
125
metric = QStyle::PM_LargeIconSize;
126
} else if (toolBarRadioButton->isChecked()) {
127
metric = QStyle::PM_ToolBarIconSize;
128
} else if (listViewRadioButton->isChecked()) {
129
metric = QStyle::PM_ListViewIconSize;
131
metric = QStyle::PM_IconViewIconSize;
133
extent = QApplication::style()->pixelMetric(metric);
135
previewArea->setSize(QSize(extent, extent));
136
otherSpinBox->setEnabled(otherRadioButton->isChecked());
139
void MainWindow::changeIcon()
143
for (int row = 0; row < imagesTable->rowCount(); ++row) {
144
QTableWidgetItem *item0 = imagesTable->item(row, 0);
145
QTableWidgetItem *item1 = imagesTable->item(row, 1);
146
QTableWidgetItem *item2 = imagesTable->item(row, 2);
148
if (item0->checkState() == Qt::Checked) {
150
if (item1->text() == tr("Normal")) {
151
mode = QIcon::Normal;
152
} else if (item1->text() == tr("Active")) {
153
mode = QIcon::Active;
155
mode = QIcon::Disabled;
159
if (item2->text() == tr("On")) {
165
QString fileName = item0->data(Qt::UserRole).toString();
166
QImage image(fileName);
168
icon.addPixmap(QPixmap::fromImage(image), mode, state);
172
previewArea->setIcon(icon);
175
void MainWindow::addImage()
177
QStringList fileNames = QFileDialog::getOpenFileNames(this,
178
tr("Open Images"), "",
179
tr("Images (*.png *.xpm *.jpg);;"
180
"All Files (*)"));
181
if (!fileNames.isEmpty()) {
182
foreach (QString fileName, fileNames) {
183
int row = imagesTable->rowCount();
184
imagesTable->setRowCount(row + 1);
186
QString imageName = QFileInfo(fileName).baseName();
187
QTableWidgetItem *item0 = new QTableWidgetItem(imageName);
188
item0->setData(Qt::UserRole, fileName);
189
item0->setFlags(item0->flags() & ~Qt::ItemIsEditable);
191
QTableWidgetItem *item1 = new QTableWidgetItem(tr("Normal"));
192
QTableWidgetItem *item2 = new QTableWidgetItem(tr("Off"));
194
if (guessModeStateAct->isChecked()) {
195
if (fileName.contains("_act")) {
196
item1->setText(tr("Active"));
197
} else if (fileName.contains("_dis")) {
198
item1->setText(tr("Disabled"));
201
if (fileName.contains("_on"))
202
item2->setText(tr("On"));
205
imagesTable->setItem(row, 0, item0);
206
imagesTable->setItem(row, 1, item1);
207
imagesTable->setItem(row, 2, item2);
208
imagesTable->openPersistentEditor(item1);
209
imagesTable->openPersistentEditor(item2);
211
item0->setCheckState(Qt::Checked);
216
void MainWindow::removeAllImages()
218
imagesTable->setRowCount(0);
222
void MainWindow::createPreviewGroupBox()
224
previewGroupBox = new QGroupBox(tr("Preview"));
226
previewArea = new IconPreviewArea;
228
QVBoxLayout *layout = new QVBoxLayout;
229
layout->addWidget(previewArea);
230
previewGroupBox->setLayout(layout);
233
void MainWindow::createImagesGroupBox()
235
imagesGroupBox = new QGroupBox(tr("Images"));
236
imagesGroupBox->setSizePolicy(QSizePolicy::Expanding,
237
QSizePolicy::Expanding);
240
labels << tr("Image") << tr("Mode") << tr("State");
242
imagesTable = new QTableWidget;
243
imagesTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
244
imagesTable->setSelectionMode(QAbstractItemView::NoSelection);
245
imagesTable->setColumnCount(3);
246
imagesTable->setHorizontalHeaderLabels(labels);
247
imagesTable->setItemDelegate(new ImageDelegate(this));
249
imagesTable->horizontalHeader()->resizeSection(0, 160);
250
imagesTable->horizontalHeader()->resizeSection(1, 80);
251
imagesTable->horizontalHeader()->resizeSection(2, 80);
252
imagesTable->verticalHeader()->hide();
254
connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem *)),
255
this, SLOT(changeIcon()));
257
QVBoxLayout *layout = new QVBoxLayout;
258
layout->addWidget(imagesTable);
259
imagesGroupBox->setLayout(layout);
262
void MainWindow::createIconSizeGroupBox()
264
iconSizeGroupBox = new QGroupBox(tr("Icon Size"));
266
smallRadioButton = new QRadioButton;
267
largeRadioButton = new QRadioButton;
268
toolBarRadioButton = new QRadioButton;
269
listViewRadioButton = new QRadioButton;
270
iconViewRadioButton = new QRadioButton;
271
otherRadioButton = new QRadioButton(tr("Other:"));
273
otherSpinBox = new IconSizeSpinBox;
274
otherSpinBox->setRange(8, 128);
275
otherSpinBox->setValue(64);
277
connect(toolBarRadioButton, SIGNAL(toggled(bool)),
278
this, SLOT(changeSize()));
279
connect(listViewRadioButton, SIGNAL(toggled(bool)),
280
this, SLOT(changeSize()));
281
connect(iconViewRadioButton, SIGNAL(toggled(bool)),
282
this, SLOT(changeSize()));
283
connect(smallRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
284
connect(largeRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
285
connect(otherRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
286
connect(otherSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSize()));
288
QHBoxLayout *otherSizeLayout = new QHBoxLayout;
289
otherSizeLayout->addWidget(otherRadioButton);
290
otherSizeLayout->addWidget(otherSpinBox);
292
QGridLayout *layout = new QGridLayout;
293
layout->addWidget(smallRadioButton, 0, 0);
294
layout->addWidget(largeRadioButton, 1, 0);
295
layout->addWidget(toolBarRadioButton, 2, 0);
296
layout->addWidget(listViewRadioButton, 0, 1);
297
layout->addWidget(iconViewRadioButton, 1, 1);
298
layout->addLayout(otherSizeLayout, 2, 1);
299
iconSizeGroupBox->setLayout(layout);
302
void MainWindow::createActions()
304
addImageAct = new QAction(tr("&Add Image..."), this);
305
addImageAct->setShortcut(tr("Ctrl+A"));
306
connect(addImageAct, SIGNAL(triggered()), this, SLOT(addImage()));
308
removeAllImagesAct = new QAction(tr("&Remove All Images"), this);
309
removeAllImagesAct->setShortcut(tr("Ctrl+R"));
310
connect(removeAllImagesAct, SIGNAL(triggered()),
311
this, SLOT(removeAllImages()));
313
exitAct = new QAction(tr("&Exit"), this);
314
exitAct->setShortcut(tr("Ctrl+Q"));
315
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
317
styleActionGroup = new QActionGroup(this);
318
foreach (QString styleName, QStyleFactory::keys()) {
319
QAction *action = new QAction(styleActionGroup);
320
action->setText(tr("%1 Style").arg(styleName));
321
action->setData(styleName);
322
action->setCheckable(true);
323
connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle(bool)));
326
guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this);
327
guessModeStateAct->setCheckable(true);
328
guessModeStateAct->setChecked(true);
330
aboutAct = new QAction(tr("&About"), this);
331
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
333
aboutQtAct = new QAction(tr("About &Qt"), this);
334
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
337
void MainWindow::createMenus()
339
fileMenu = menuBar()->addMenu(tr("&File"));
340
fileMenu->addAction(addImageAct);
341
fileMenu->addAction(removeAllImagesAct);
342
fileMenu->addSeparator();
343
fileMenu->addAction(exitAct);
345
viewMenu = menuBar()->addMenu(tr("&View"));
346
foreach (QAction *action, styleActionGroup->actions())
347
viewMenu->addAction(action);
348
viewMenu->addSeparator();
349
viewMenu->addAction(guessModeStateAct);
351
menuBar()->addSeparator();
353
helpMenu = menuBar()->addMenu(tr("&Help"));
354
helpMenu->addAction(aboutAct);
355
helpMenu->addAction(aboutQtAct);
358
void MainWindow::createContextMenu()
360
imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu);
361
imagesTable->addAction(addImageAct);
362
imagesTable->addAction(removeAllImagesAct);
365
void MainWindow::checkCurrentStyle()
367
foreach (QAction *action, styleActionGroup->actions()) {
368
QString styleName = action->data().toString();
369
QStyle *candidate = QStyleFactory::create(styleName);
371
if (candidate->metaObject()->className()
372
== QApplication::style()->metaObject()->className()) {
373
action->trigger();
379
<p /><address><hr /><div align="center">
380
<table width="100%" cellspacing="0" border="0"><tr class="address">
381
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
382
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
383
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
384
</tr></table></div></address></body>