source: trunk/doc/src/snippets/qtablewidget-using/mainwindow.cpp@ 624

Last change on this file since 624 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include "math.h"
44
45#include "mainwindow.h"
46
47MainWindow::MainWindow()
48{
49 QMenu *fileMenu = new QMenu(tr("&File"));
50
51 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
52 quitAction->setShortcut(tr("Ctrl+Q"));
53
54 QMenu *itemsMenu = new QMenu(tr("&Items"));
55
56 QAction *sumItemsAction = itemsMenu->addAction(tr("&Sum Items"));
57 QAction *averageItemsAction = itemsMenu->addAction(tr("&Average Items"));
58
59 menuBar()->addMenu(fileMenu);
60 menuBar()->addMenu(itemsMenu);
61
62//! [0]
63 tableWidget = new QTableWidget(12, 3, this);
64//! [0]
65 tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
66
67//! [1]
68 QTableWidgetItem *valuesHeaderItem = new QTableWidgetItem(tr("Values"));
69 tableWidget->setHorizontalHeaderItem(0, valuesHeaderItem);
70//! [1]
71 valuesHeaderItem->setTextAlignment(Qt::AlignVCenter);
72 QTableWidgetItem *squaresHeaderItem = new QTableWidgetItem(tr("Squares"));
73 squaresHeaderItem->setIcon(QIcon(QPixmap(":/Images/squared.png")));
74 squaresHeaderItem->setTextAlignment(Qt::AlignVCenter);
75//! [2]
76 QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));
77 cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png")));
78 cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
79//! [2]
80 tableWidget->setHorizontalHeaderItem(1, squaresHeaderItem);
81 tableWidget->setHorizontalHeaderItem(2, cubesHeaderItem);
82
83 connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
84 connect(sumItemsAction, SIGNAL(triggered()), this, SLOT(sumItems()));
85 connect(averageItemsAction, SIGNAL(triggered()), this, SLOT(averageItems()));
86
87 setupTableItems();
88
89 setCentralWidget(tableWidget);
90 setWindowTitle(tr("Table Widget"));
91}
92
93void MainWindow::setupTableItems()
94{
95 for (int row = 0; row < tableWidget->rowCount()-1; ++row) {
96 for (int column = 0; column < tableWidget->columnCount(); ++column) {
97//! [3]
98 QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
99 pow(row, column+1)));
100 tableWidget->setItem(row, column, newItem);
101//! [3]
102 }
103 }
104 for (int column = 0; column < tableWidget->columnCount(); ++column) {
105 QTableWidgetItem *newItem = new QTableWidgetItem;
106 newItem->setFlags(Qt::ItemIsEnabled);
107 tableWidget->setItem(tableWidget->rowCount()-1, column, newItem);
108 }
109}
110
111void MainWindow::averageItems()
112{
113 QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
114 QTableWidgetItem *item;
115 int number = 0;
116 double total = 0;
117
118 foreach (item, selected) {
119 bool ok;
120 double value = item->text().toDouble(&ok);
121
122 if (ok && !item->text().isEmpty()) {
123 total += value;
124 number++;
125 }
126 }
127 if (number > 0)
128 tableWidget->currentItem()->setText(QString::number(total/number));
129}
130
131void MainWindow::sumItems()
132{
133//! [4]
134 QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
135 QTableWidgetItem *item;
136 int number = 0;
137 double total = 0;
138
139 foreach (item, selected) {
140 bool ok;
141 double value = item->text().toDouble(&ok);
142
143 if (ok && !item->text().isEmpty()) {
144 total += value;
145 number++;
146 }
147 }
148//! [4]
149 if (number > 0)
150 tableWidget->currentItem()->setText(QString::number(total));
151}
Note: See TracBrowser for help on using the repository browser.