5
6
7
8
9
11#include <QAbstractItemModel>
12#include <QItemSelection>
13#include <QItemSelectionModel>
19#include "../include/mainwindow.h"
20#include "../common-table-model/model.h"
25 setWindowTitle(
"Selected Items in a Table Model");
27 model =
new TableModel(8, 4,
this);
29 table =
new QTableView(
this);
30 table->setModel(model);
32 QMenu *actionMenu =
new QMenu(tr(
"&Actions"),
this);
33 QAction *fillAction = actionMenu->addAction(tr(
"&Fill Selection"));
34 QAction *clearAction = actionMenu->addAction(tr(
"&Clear Selection"));
35 QAction *selectAllAction = actionMenu->addAction(tr(
"&Select All"));
36 menuBar()->addMenu(actionMenu);
38 connect(fillAction, &QAction::triggered,
this, &MainWindow::fillSelection);
39 connect(clearAction, &QAction::triggered,
this, &MainWindow::clearSelection);
40 connect(selectAllAction, &QAction::triggered,
this, &MainWindow::selectAll);
42 selectionModel = table->selectionModel();
45 setCentralWidget(table);
51 const QModelIndexList indexes = selectionModel->selectedIndexes();
53 for (
const QModelIndex &index : indexes) {
54 QString text = QString(
"(%1,%2)").arg(index.row()).arg(index.column());
55 model->setData(index, text);
62 const QModelIndexList indexes = selectionModel->selectedIndexes();
64 for (
const QModelIndex &index : indexes)
65 model->setData(index, QString());
71 QModelIndex parent = QModelIndex();
75 QModelIndex topLeft = model->index(0, 0, parent);
76 QModelIndex bottomRight = model->index(model->rowCount(parent)-1,
77 model->columnCount(parent)-1, parent);
81 QItemSelection selection(topLeft, bottomRight);
82 selectionModel->select(selection, QItemSelectionModel::Select);
MainWindow(QWidget *parent=nullptr)