Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
22 : QMainWindow(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{
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 }
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{
72 QModelIndex topLeft = model->index(0, 0, parent);
73 QModelIndex bottomRight = model->index(model->rowCount(parent)-1,
74 model->columnCount(parent)-1, parent);
76
78 QItemSelection selection(topLeft, bottomRight);
79 selectionModel->select(selection, QItemSelectionModel::Select);
81}
virtual Q_INVOKABLE int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
virtual Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
virtual Q_INVOKABLE int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
virtual Q_INVOKABLE QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index.
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void triggered(bool checked=false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a m...
QModelIndexList selectedIndexes
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Selects the model item index using the specified command, and emits selectionChanged().
\inmodule QtCore
The QMainWindow class provides a main application window.
Definition qmainwindow.h:25
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:768
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
\inmodule QtCore
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8870
The QTableView class provides a default model/view implementation of a table view.
Definition qtableview.h:18
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
[0]
Definition model.h:12
QString text
GLuint index
[2]
GLenum GLenum GLsizei void * table
#define tr(X)
QSqlQueryModel * model
[16]
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
mimeData setData("text/csv", csvData)
QMenuBar * menuBar
[0]
statusBar() -> addWidget(new MyReadWriteIndication)
[0]