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 <QStatusBar>
15#include <QTableView>
16
17#include "../include/mainwindow.h"
18#include "../common-table-model/model.h"
19
22{
23 setWindowTitle("Selected items in a table model");
24
25 model = new TableModel(8, 4, this);
26
27 table = new QTableView(this);
28 table->setModel(model);
29
30 selectionModel = table->selectionModel();
31 connect(selectionModel, &QItemSelectionModel::selectionChanged,
32 this, &MainWindow::updateSelection);
33 connect(selectionModel, &QItemSelectionModel::currentChanged,
34 this, &MainWindow::changeCurrent);
35
36 statusBar();
37 setCentralWidget(table);
38}
39
40//! [0]
41void MainWindow::updateSelection(const QItemSelection &selected,
42 const QItemSelection &deselected)
43{
44 QModelIndexList items = selected.indexes();
45
46 for (const QModelIndex &index : std::as_const(items)) {
47 QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
48 model->setData(index, text);
49//! [0] //! [1]
50 }
51//! [1]
52
53//! [2]
54 items = deselected.indexes();
55
56 for (const QModelIndex &index : std::as_const(items)) {
57 model->setData(index, QString());
58 }
59}
60//! [2]
61
62//! [3]
63void MainWindow::changeCurrent(const QModelIndex &current,
64 const QModelIndex &previous)
65{
66 statusBar()->showMessage(
67 tr("Moved from (%1,%2) to (%3,%4)")
68 .arg(previous.row()).arg(previous.column())
69 .arg(current.row()).arg(current.column()));
70}
71//! [3]
MainWindow(QWidget *parent=nullptr)
void changeCurrent(const QModelIndex &current, const QModelIndex &previous)
[2]
Definition window.cpp:63
void updateSelection(const QItemSelection &selected, const QItemSelection &deselected)
[0]
Definition window.cpp:41
friend class QWidget
Definition qpainter.h:431