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 *tableMenu = new QMenu(tr("&Table"));
16
17 QAction *tableWidthAction = tableMenu->addAction(tr("Change Table &Width"));
18 QAction *tableHeightAction = tableMenu->addAction(tr("Change Table &Height"));
19
20 menuBar()->addMenu(fileMenu);
21 menuBar()->addMenu(tableMenu);
22
23//! [0]
24 tableWidget = new QTableWidget(this);
25//! [0]
26 tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
27
28 connect(quitAction, &QAction::triggered, this, &QWidget::close);
29 connect(tableWidthAction, &QAction::triggered, this, &MainWindow::changeWidth);
30 connect(tableHeightAction, &QAction::triggered, this, &MainWindow::changeHeight);
31
32 setupTableItems();
33
34 setCentralWidget(tableWidget);
35 setWindowTitle(tr("Table Widget Resizing"));
36}
37
38void MainWindow::setupTableItems()
39{
40//! [1]
41 tableWidget->setRowCount(10);
42 tableWidget->setColumnCount(5);
43//! [1]
44
45 for (int row = 0; row < tableWidget->rowCount(); ++row) {
46 for (int column = 0; column < tableWidget->columnCount(); ++column) {
47//! [2]
48 QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
49 (row+1)*(column+1)));
50 tableWidget->setItem(row, column, newItem);
51//! [2]
52 }
53 }
54}
55
56void MainWindow::changeWidth()
57{
58 bool ok;
59
60 int newWidth = QInputDialog::getInteger(this, tr("Change table width"),
61 tr("Input the number of columns required (1-20):"),
62 tableWidget->columnCount(), 1, 20, 1, &ok);
63
64 if (ok)
65 tableWidget->setColumnCount(newWidth);
66}
67
68void MainWindow::changeHeight()
69{
70 bool ok;
71
72 int newHeight = QInputDialog::getInteger(this, tr("Change table height"),
73 tr("Input the number of rows required (1-20):"),
74 tableWidget->rowCount(), 1, 20, 1, &ok);
75
76 if (ok)
77 tableWidget->setRowCount(newHeight);
78}