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