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#include <math.h>
6
7#include "mainwindow.h"
8
10{
11 QMenu *fileMenu = new QMenu(tr("&File"));
12
13 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
14 quitAction->setShortcut(tr("Ctrl+Q"));
15
16 QMenu *itemsMenu = new QMenu(tr("&Items"));
17
18 QAction *sumItemsAction = itemsMenu->addAction(tr("&Sum Items"));
19 QAction *averageItemsAction = itemsMenu->addAction(tr("&Average Items"));
20
21 menuBar()->addMenu(fileMenu);
22 menuBar()->addMenu(itemsMenu);
23
24//! [0]
25 tableWidget = new QTableWidget(12, 3, this);
26//! [0]
27 tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
28
29//! [1]
30 QTableWidgetItem *valuesHeaderItem = new QTableWidgetItem(tr("Values"));
31 tableWidget->setHorizontalHeaderItem(0, valuesHeaderItem);
32//! [1]
33 valuesHeaderItem->setTextAlignment(Qt::AlignVCenter);
34 QTableWidgetItem *squaresHeaderItem = new QTableWidgetItem(tr("Squares"));
35 squaresHeaderItem->setIcon(QIcon(QPixmap(":/Images/squared.png")));
36 squaresHeaderItem->setTextAlignment(Qt::AlignVCenter);
37//! [2]
38 QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));
39 cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png")));
40 cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
41//! [2]
42 tableWidget->setHorizontalHeaderItem(1, squaresHeaderItem);
43 tableWidget->setHorizontalHeaderItem(2, cubesHeaderItem);
44
45 connect(quitAction, &QAction::triggered, this, &QWidget::close);
46 connect(sumItemsAction, &QAction::triggered, this, &MainWindow::sumItems);
47 connect(averageItemsAction, &QAction::triggered, this, &MainWindow::averageItems);
48
49 setupTableItems();
50
51 setCentralWidget(tableWidget);
52 setWindowTitle(tr("Table Widget"));
53}
54
55void MainWindow::setupTableItems()
56{
57 for (int row = 0; row < tableWidget->rowCount()-1; ++row) {
58 for (int column = 0; column < tableWidget->columnCount(); ++column) {
59//! [3]
60 QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
61 pow(row, column+1)));
62 tableWidget->setItem(row, column, newItem);
63//! [3]
64 }
65 }
66 for (int column = 0; column < tableWidget->columnCount(); ++column) {
67 QTableWidgetItem *newItem = new QTableWidgetItem;
68 newItem->setFlags(Qt::ItemIsEnabled);
69 tableWidget->setItem(tableWidget->rowCount()-1, column, newItem);
70 }
71}
72
73void MainWindow::averageItems()
74{
75 const QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
76 int number = 0;
77 double total = 0;
78
79 for (QTableWidgetItem *item : selected) {
80 bool ok;
81 double value = item->text().toDouble(&ok);
82
83 if (ok && !item->text().isEmpty()) {
84 total += value;
85 number++;
86 }
87 }
88 if (number > 0)
89 tableWidget->currentItem()->setText(QString::number(total/number));
90}
91
93{
94//! [4]
95 const QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
96 int number = 0;
97 double total = 0;
98
99 for (QTableWidgetItem *item : selected) {
100 bool ok;
101 double value = item->text().toDouble(&ok);
102
103 if (ok && !item->text().isEmpty()) {
104 total += value;
105 number++;
106 }
107 }
108//! [4]
109 if (number > 0)
110 tableWidget->currentItem()->setText(QString::number(total));
111}
void sumItems()