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
window.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/*
5 window.cpp
6
7 A minimal subclass of QTableView with slots to allow the selection model
8 to be monitored.
9*/
10
11#include <QAbstractItemModel>
12#include <QItemSelection>
13#include <QItemSelectionModel>
14#include <QMenu>
15#include <QMenuBar>
16#include <QStatusBar>
17#include <QTableView>
18
19#include "../include/mainwindow.h"
20#include "../common-table-model/model.h"
21
24{
25 setWindowTitle("Selected Items in a Table Model");
26
27 model = new TableModel(8, 4, this);
28
29 table = new QTableView(this);
30 table->setModel(model);
31
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);
37
38 connect(fillAction, &QAction::triggered, this, &MainWindow::fillSelection);
39 connect(clearAction, &QAction::triggered, this, &MainWindow::clearSelection);
40 connect(selectAllAction, &QAction::triggered, this, &MainWindow::selectAll);
41
42 selectionModel = table->selectionModel();
43
44 statusBar();
45 setCentralWidget(table);
46}
47
48void MainWindow::fillSelection()
49{
50 //! [0]
51 const QModelIndexList indexes = selectionModel->selectedIndexes();
52
53 for (const QModelIndex &index : indexes) {
54 QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
55 model->setData(index, text);
56 }
57 //! [0]
58}
59
60void MainWindow::clearSelection()
61{
62 const QModelIndexList indexes = selectionModel->selectedIndexes();
63
64 for (const QModelIndex &index : indexes)
65 model->setData(index, QString());
66}
67
68void MainWindow::selectAll()
69{
70 //! [1]
71 QModelIndex parent = QModelIndex();
72 //! [1]
73
74 //! [2]
75 QModelIndex topLeft = model->index(0, 0, parent);
76 QModelIndex bottomRight = model->index(model->rowCount(parent)-1,
77 model->columnCount(parent)-1, parent);
78 //! [2]
79
80 //! [3]
81 QItemSelection selection(topLeft, bottomRight);
82 selectionModel->select(selection, QItemSelectionModel::Select);
83 //! [3]
84}
MainWindow(QWidget *parent=nullptr)
friend class QWidget
Definition qpainter.h:431