source: trunk/doc/src/snippets/qtablewidget-resizing/mainwindow.cpp@ 651

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

trunk: Merged in qt 4.6.2 sources.

File size: 3.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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
44#include "mainwindow.h"
45
46MainWindow::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 *tableMenu = new QMenu(tr("&Table"));
54
55 QAction *tableWidthAction = tableMenu->addAction(tr("Change Table &Width"));
56 QAction *tableHeightAction = tableMenu->addAction(tr("Change Table &Height"));
57
58 menuBar()->addMenu(fileMenu);
59 menuBar()->addMenu(tableMenu);
60
61//! [0]
62 tableWidget = new QTableWidget(this);
63//! [0]
64 tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
65
66 connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
67 connect(tableWidthAction, SIGNAL(triggered()), this, SLOT(changeWidth()));
68 connect(tableHeightAction, SIGNAL(triggered()), this, SLOT(changeHeight()));
69
70 setupTableItems();
71
72 setCentralWidget(tableWidget);
73 setWindowTitle(tr("Table Widget Resizing"));
74}
75
76void MainWindow::setupTableItems()
77{
78//! [1]
79 tableWidget->setRowCount(10);
80 tableWidget->setColumnCount(5);
81//! [1]
82
83 for (int row = 0; row < tableWidget->rowCount(); ++row) {
84 for (int column = 0; column < tableWidget->columnCount(); ++column) {
85//! [2]
86 QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
87 (row+1)*(column+1)));
88 tableWidget->setItem(row, column, newItem);
89//! [2]
90 }
91 }
92}
93
94void MainWindow::changeWidth()
95{
96 bool ok;
97
98 int newWidth = QInputDialog::getInteger(this, tr("Change table width"),
99 tr("Input the number of columns required (1-20):"),
100 tableWidget->columnCount(), 1, 20, 1, &ok);
101
102 if (ok)
103 tableWidget->setColumnCount(newWidth);
104}
105
106void MainWindow::changeHeight()
107{
108 bool ok;
109
110 int newHeight = QInputDialog::getInteger(this, tr("Change table height"),
111 tr("Input the number of rows required (1-20):"),
112 tableWidget->rowCount(), 1, 20, 1, &ok);
113
114 if (ok)
115 tableWidget->setRowCount(newHeight);
116}
Note: See TracBrowser for help on using the repository browser.