Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
24 tableWidget = new QTableWidget(this);
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{
41 tableWidget->setRowCount(10);
42 tableWidget->setColumnCount(5);
44
45 for (int row = 0; row < tableWidget->rowCount(); ++row) {
46 for (int column = 0; column < tableWidget->columnCount(); ++column) {
48 QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
49 (row+1)*(column+1)));
50 tableWidget->setItem(row, column, newItem);
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}
void setSelectionMode(QAbstractItemView::SelectionMode mode)
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void triggered(bool checked=false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a m...
void setCentralWidget(QWidget *widget)
Sets the given widget to be the main window's central widget.
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:768
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QTableWidgetItem class provides an item for use with the QTableWidget class.
The QTableWidget class provides an item-based table view with a default model.
void setRowCount(int rows)
Sets the number of rows in this table's model to rows.
void setColumnCount(int columns)
Sets the number of columns in this table's model to columns.
void setItem(int row, int column, QTableWidgetItem *item)
Sets the item for the given row and column to item.
int rowCount
the number of rows in the table
int columnCount
the number of columns in the table
bool close()
Closes this widget.
Definition qwidget.cpp:8562
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
SSL_CTX int void * arg
#define tr(X)
QMenuBar * menuBar
[0]