Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
main.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 main.cpp
6
7 A simple example that shows how selections can be used directly on a model.
8 It shows the result of some selections made using a table view.
9*/
10
11#include <QApplication>
12#include <QItemSelection>
13#include <QItemSelectionModel>
14#include <QTableView>
15
16#include "model.h"
17
18int main(int argc, char *argv[])
19{
20 QApplication app(argc, argv);
21
23 TableModel *model = new TableModel(8, 4, &app);
24
25 QTableView *table = new QTableView(0);
26 table->setModel(model);
27
28 QItemSelectionModel *selectionModel = table->selectionModel();
30 QModelIndex topLeft;
31 QModelIndex bottomRight;
32
33 topLeft = model->index(0, 0, QModelIndex());
34 bottomRight = model->index(5, 2, QModelIndex());
36
38 QItemSelection selection(topLeft, bottomRight);
41
43 QItemSelection toggleSelection;
44
45 topLeft = model->index(2, 1, QModelIndex());
46 bottomRight = model->index(7, 3, QModelIndex());
47 toggleSelection.select(topLeft, bottomRight);
48
49 selectionModel->select(toggleSelection, QItemSelectionModel::Toggle);
51
53 QItemSelection columnSelection;
54
55 topLeft = model->index(0, 1, QModelIndex());
56 bottomRight = model->index(0, 2, QModelIndex());
57
58 columnSelection.select(topLeft, bottomRight);
59
60 selectionModel->select(columnSelection,
62
63 QItemSelection rowSelection;
64
65 topLeft = model->index(0, 0, QModelIndex());
66 bottomRight = model->index(1, 0, QModelIndex());
67
68 rowSelection.select(topLeft, bottomRight);
69
70 selectionModel->select(rowSelection,
73
74 table->setWindowTitle("Selected items in a table model");
75 table->show();
76 table->resize(460, 280);
77 return app.exec();
78}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Selects the model item index using the specified command, and emits selectionChanged().
\inmodule QtCore
Q_CORE_EXPORT void select(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Adds the items in the range that extends from the top-left model item, specified by the topLeft index...
\inmodule QtCore
The QTableView class provides a default model/view implementation of a table view.
Definition qtableview.h:18
[0]
Definition model.h:12
int main()
[0]
GLenum GLenum GLsizei void * table
QSqlQueryModel * model
[16]
QItemSelection * selection
[0]
QApplication app(argc, argv)
[0]