8MainWindow::MainWindow()
10 QMenu *fileMenu =
new QMenu(tr(
"&File"));
12 QAction *quitAction = fileMenu->addAction(tr(
"E&xit"));
13 quitAction->setShortcut(tr(
"Ctrl+Q"));
15 QMenu *tableMenu =
new QMenu(tr(
"&Table"));
17 QAction *tableWidthAction = tableMenu->addAction(tr(
"Change Table &Width"));
18 QAction *tableHeightAction = tableMenu->addAction(tr(
"Change Table &Height"));
20 menuBar()->addMenu(fileMenu);
21 menuBar()->addMenu(tableMenu);
24 tableWidget =
new QTableWidget(
this);
26 tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
28 connect(quitAction, &QAction::triggered,
this, &QWidget::close);
29 connect(tableWidthAction, &QAction::triggered,
this, &MainWindow::changeWidth);
30 connect(tableHeightAction, &QAction::triggered,
this, &MainWindow::changeHeight);
34 setCentralWidget(tableWidget);
35 setWindowTitle(tr(
"Table Widget Resizing"));
38void MainWindow::setupTableItems()
41 tableWidget->setRowCount(10);
42 tableWidget->setColumnCount(5);
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(
50 tableWidget->setItem(row, column, newItem);
56void MainWindow::changeWidth()
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);
65 tableWidget->setColumnCount(newWidth);
68void MainWindow::changeHeight()
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);
77 tableWidget->setRowCount(newHeight);