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
18#include "model.h"
19#include "window.h"
20
21MainWindow::MainWindow(QWidget *parent)
23{
24 setWindowTitle("Selected Items in a Table Model");
25
26 model = new TableModel(8, 4, this);
27
28 table = new QTableView(this);
29 table->setModel(model);
30
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);
36
37 connect(fillAction, &QAction::triggered, this, &MainWindow::fillSelection);
38 connect(clearAction, &QAction::triggered, this, &MainWindow::clearSelection);
39 connect(selectAllAction, &QAction::triggered, this, &MainWindow::selectAll);
40
41 selectionModel = table->selectionModel();
42
43 statusBar();
44 setCentralWidget(table);
45}
46
47void MainWindow::fillSelection()
48{
49//! [0]
50 const QModelIndexList indexes = selectionModel->selectedIndexes();
51
52 for (const QModelIndex &index : indexes) {
53 QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
54 model->setData(index, text);
55 }
56//! [0]
57}
58
59void MainWindow::clearSelection()
60{
61 const QModelIndexList indexes = selectionModel->selectedIndexes();
62
63 for (const QModelIndex &index : indexes)
64 model->setData(index, QString());
65}
66
67void MainWindow::selectAll()
68{
69//! [1]
70 QModelIndex parent = QModelIndex();
71//! [1] //! [2]
72 QModelIndex topLeft = model->index(0, 0, parent);
73 QModelIndex bottomRight = model->index(model->rowCount(parent)-1,
74 model->columnCount(parent)-1, parent);
75//! [2]
76
77//! [3]
78 QItemSelection selection(topLeft, bottomRight);
79 selectionModel->select(selection, QItemSelectionModel::Select);
80//! [3]
81}
MainWindow(QWidget *parent=nullptr)