5
6
7
8
9
11#include <QAbstractItemModel>
12#include <QItemSelection>
13#include <QItemSelectionModel>
24 setWindowTitle(
"Selected Items in a Table Model");
26 model =
new TableModel(8, 4,
this);
28 table =
new QTableView(
this);
29 table->setModel(model);
31 QMenu *actionMenu =
new QMenu(tr(
"&Actions"),
this);
32 QAction *fillAction = actionMenu->addAction(tr(
"&Fill Selection"));
33 QAction *clearAction = actionMenu->addAction(tr(
"&Clear Selection"));
34 QAction *selectAllAction = actionMenu->addAction(tr(
"&Select All"));
35 menuBar()->addMenu(actionMenu);
37 connect(fillAction, &QAction::triggered,
this, &MainWindow::fillSelection);
38 connect(clearAction, &QAction::triggered,
this, &MainWindow::clearSelection);
39 connect(selectAllAction, &QAction::triggered,
this, &MainWindow::selectAll);
41 selectionModel = table->selectionModel();
44 setCentralWidget(table);
50 const QModelIndexList indexes = selectionModel->selectedIndexes();
52 for (
const QModelIndex &index : indexes) {
53 QString text = QString(
"(%1,%2)").arg(index.row()).arg(index.column());
54 model->setData(index, text);
61 const QModelIndexList indexes = selectionModel->selectedIndexes();
63 for (
const QModelIndex &index : indexes)
64 model->setData(index, QString());
70 QModelIndex parent = QModelIndex();
72 QModelIndex topLeft = model->index(0, 0, parent);
73 QModelIndex bottomRight = model->index(model->rowCount(parent)-1,
74 model->columnCount(parent)-1, parent);
78 QItemSelection selection(topLeft, bottomRight);
79 selectionModel->select(selection, QItemSelectionModel::Select);
MainWindow(QWidget *parent=nullptr)