11 QMenu *fileMenu =
new QMenu(tr(
"&File"));
13 QAction *quitAction = fileMenu->addAction(tr(
"E&xit"));
14 quitAction->setShortcut(tr(
"Ctrl+Q"));
16 QMenu *itemsMenu =
new QMenu(tr(
"&Items"));
18 QAction *sumItemsAction = itemsMenu->addAction(tr(
"&Sum Items"));
19 QAction *averageItemsAction = itemsMenu->addAction(tr(
"&Average Items"));
21 menuBar()->addMenu(fileMenu);
22 menuBar()->addMenu(itemsMenu);
25 tableWidget =
new QTableWidget(12, 3,
this);
27 tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
30 QTableWidgetItem *valuesHeaderItem =
new QTableWidgetItem(tr(
"Values"));
31 tableWidget->setHorizontalHeaderItem(0, valuesHeaderItem);
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);
38 QTableWidgetItem *cubesHeaderItem =
new QTableWidgetItem(tr(
"Cubes"));
39 cubesHeaderItem->setIcon(QIcon(QPixmap(
":/Images/cubed.png")));
40 cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
42 tableWidget->setHorizontalHeaderItem(1, squaresHeaderItem);
43 tableWidget->setHorizontalHeaderItem(2, cubesHeaderItem);
45 connect(quitAction, &QAction::triggered,
this, &QWidget::close);
46 connect(sumItemsAction, &QAction::triggered,
this, &MainWindow::sumItems);
47 connect(averageItemsAction, &QAction::triggered,
this, &MainWindow::averageItems);
51 setCentralWidget(tableWidget);
52 setWindowTitle(tr(
"Table Widget"));
57 for (
int row = 0; row < tableWidget->rowCount()-1; ++row) {
58 for (
int column = 0; column < tableWidget->columnCount(); ++column) {
60 QTableWidgetItem *newItem =
new QTableWidgetItem(tr(
"%1").arg(
62 tableWidget->setItem(row, column, newItem);
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);
75 const QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
79 for (QTableWidgetItem *item : selected) {
81 double value = item->text().toDouble(&ok);
83 if (ok && !item->text().isEmpty()) {
89 tableWidget->currentItem()->setText(QString::number(total/number));
95 const QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
99 for (QTableWidgetItem *item : selected) {
101 double value = item->text().toDouble(&ok);
103 if (ok && !item->text().isEmpty()) {
110 tableWidget->currentItem()->setText(QString::number(total));