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
qitemselectionmodel.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QITEMSELECTIONMODEL_H
5#define QITEMSELECTIONMODEL_H
6
7#include <QtCore/qglobal.h>
8
9#include <QtCore/qabstractitemmodel.h>
10#include <QtCore/qlist.h>
11#include <QtCore/qset.h>
12
14
16
17class Q_CORE_EXPORT QItemSelectionRange
18{
19
20public:
22 QItemSelectionRange(const QModelIndex &topL, const QModelIndex &bottomR) : tl(topL), br(bottomR) {}
23 explicit QItemSelectionRange(const QModelIndex &index) : tl(index), br(tl) {}
24
26 {
27 tl.swap(other.tl);
28 br.swap(other.br);
29 }
30
31 inline int top() const { return tl.row(); }
32 inline int left() const { return tl.column(); }
33 inline int bottom() const { return br.row(); }
34 inline int right() const { return br.column(); }
35 inline int width() const { return br.column() - tl.column() + 1; }
36 inline int height() const { return br.row() - tl.row() + 1; }
37
38 inline const QPersistentModelIndex &topLeft() const { return tl; }
39 inline const QPersistentModelIndex &bottomRight() const { return br; }
40 inline QModelIndex parent() const { return tl.parent(); }
41 inline const QAbstractItemModel *model() const { return tl.model(); }
42
43 inline bool contains(const QModelIndex &index) const
44 {
45 return (parent() == index.parent()
46 && tl.row() <= index.row() && tl.column() <= index.column()
47 && br.row() >= index.row() && br.column() >= index.column());
48 }
49
50 inline bool contains(int row, int column, const QModelIndex &parentIndex) const
51 {
52 return (parent() == parentIndex
53 && tl.row() <= row && tl.column() <= column
54 && br.row() >= row && br.column() >= column);
55 }
56
57 bool intersects(const QItemSelectionRange &other) const;
58 QItemSelectionRange intersected(const QItemSelectionRange &other) const;
59
60#if QT_CORE_REMOVED_SINCE(6, 8)
61 inline bool operator==(const QItemSelectionRange &other) const
62 { return comparesEqual(*this, other); }
63 inline bool operator!=(const QItemSelectionRange &other) const
64 { return !operator==(other); }
65#endif
66 inline bool isValid() const
67 {
68 return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
69 && top() <= bottom() && left() <= right());
70 }
71
72 bool isEmpty() const;
73
74 QModelIndexList indexes() const;
75
76private:
77 friend bool comparesEqual(const QItemSelectionRange &lhs,
78 const QItemSelectionRange &rhs) noexcept
79 {
80 return (lhs.tl == rhs.tl && lhs.br == rhs.br);
81 }
84};
86
87class QItemSelection;
89
90class Q_CORE_EXPORT QItemSelectionModel : public QObject
91{
93 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged
94 BINDABLE bindableModel)
95 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false
96 DESIGNABLE false)
97 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false
98 DESIGNABLE false)
99 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false
100 DESIGNABLE false)
101 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged
102 STORED false DESIGNABLE false)
103
104 Q_DECLARE_PRIVATE(QItemSelectionModel)
105
106public:
107
109 NoUpdate = 0x0000,
110 Clear = 0x0001,
111 Select = 0x0002,
112 Deselect = 0x0004,
113 Toggle = 0x0008,
114 Current = 0x0010,
115 Rows = 0x0020,
116 Columns = 0x0040,
117 SelectCurrent = Select | Current,
118 ToggleCurrent = Toggle | Current,
119 ClearAndSelect = Clear | Select
120 };
121
122 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
123 Q_FLAG(SelectionFlags)
124
125 explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
127 virtual ~QItemSelectionModel();
128
129 QModelIndex currentIndex() const;
130
131 Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
132 Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const;
133 Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const;
134
135 Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const;
136 Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const;
137
138 bool hasSelection() const;
139
140 QModelIndexList selectedIndexes() const;
141 Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const;
142 Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const;
143 const QItemSelection selection() const;
144
145 const QAbstractItemModel *model() const;
147 QBindable<QAbstractItemModel *> bindableModel();
148
150
151public Q_SLOTS:
152 virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
153 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
154 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
155 virtual void clear();
156 virtual void reset();
157
158 void clearSelection();
159 virtual void clearCurrentIndex();
160
162 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
163 void currentChanged(const QModelIndex &current, const QModelIndex &previous);
164 void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
165 void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
167
168protected:
170 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
171
172private:
173 Q_DISABLE_COPY(QItemSelectionModel)
174};
175
176Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
177
178// We export each out-of-line method individually to prevent MSVC from
179// exporting the whole QList class.
181{
182public:
184 Q_CORE_EXPORT QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
185
186 // reusing QList::swap() here is OK!
187
188 Q_CORE_EXPORT void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
189 Q_CORE_EXPORT bool contains(const QModelIndex &index) const;
190 Q_CORE_EXPORT QModelIndexList indexes() const;
191 Q_CORE_EXPORT void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
192 Q_CORE_EXPORT static void split(const QItemSelectionRange &range,
195};
196Q_DECLARE_SHARED(QItemSelection)
197
198#ifndef QT_NO_DEBUG_STREAM
199Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
200#endif
201
203
206
207#endif // QITEMSELECTIONMODEL_H
\inmodule QtCore
SelectionFlag
This enum describes the way the selection model will be updated.
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This signal is emitted whenever the selection changes.
QAbstractItemModel * model()
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
This signal is emitted whenever the current item changes.
void modelChanged(QAbstractItemModel *model)
void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous)
This signal is emitted if the current item changes and its column is different to the column of the p...
void currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
This signal is emitted if the current item changes and its row is different to the row of the previou...
bool contains(int row, int column, const QModelIndex &parentIndex) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool contains(const QModelIndex &index) const
Returns true if the model item specified by the index lies within the range of selected items; otherw...
int bottom() const
Returns the row index corresponding to the lowermost selected row in the selection range.
QItemSelectionRange(const QModelIndex &index)
Constructs a new selection range containing only the model item specified by the model index index.
const QPersistentModelIndex & topLeft() const
Returns the index for the item located at the top-left corner of the selection range.
QItemSelectionRange(const QModelIndex &topL, const QModelIndex &bottomR)
Constructs a new selection range containing only the index specified by the topLeft and the index bot...
int top() const
Returns the row index corresponding to the uppermost selected row in the selection range.
QItemSelectionRange()=default
Constructs an empty selection range.
void swap(QItemSelectionRange &other) noexcept
const QPersistentModelIndex & bottomRight() const
Returns the index for the item located at the bottom-right corner of the selection range.
int right() const
Returns the column index corresponding to the rightmost selected column in the selection range.
int height() const
Returns the number of selected rows in the selection range.
const QAbstractItemModel * model() const
Returns the model that the items in the selection range belong to.
int width() const
Returns the number of selected columns in the selection range.
friend bool comparesEqual(const QItemSelectionRange &lhs, const QItemSelectionRange &rhs) noexcept
QModelIndex parent() const
Returns the parent model item index of the items in the selection range.
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range.
bool isValid() const
Returns true if the selection range is valid; otherwise returns false.
\inmodule QtCore
\inmodule QtCore
constexpr int row() const noexcept
Returns the row this model index refers to.
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
\inmodule QtCore
Definition qobject.h:103
b clear()
Combined button and popup list for selecting options.
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
static bool hasSelection()
Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &)
QList(InputIterator, InputIterator) -> QList< ValueType >
#define QT_DECL_METATYPE_EXTERN(TYPE, EXPORT)
Definition qmetatype.h:1388
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLuint index
[2]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLsizei range
GLint left
GLint GLint bottom
GLenum GLenum GLsizei void GLsizei void * column
GLboolean reset
GLenum GLenum GLsizei void * row
GLuint64EXT * result
[6]
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
static void split(QT_FT_Vector *b)
#define QT_REQUIRE_CONFIG(feature)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_FLAG(x)
#define Q_INVOKABLE
#define Q_SLOTS
#define Q_SIGNALS
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
view setModel(model)
[17] //! [18]
QSqlQueryModel * model
[16]
QSharedPointer< T > other(t)
[5]
QItemSelection * selection
[0]
selection select(topLeft, bottomRight)