Qt 6.x
The Qt SDK
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
61 inline bool operator==(const QItemSelectionRange &other) const
62 { return (tl == other.tl && br == other.br); }
63 inline bool operator!=(const QItemSelectionRange &other) const
64 { return !operator==(other); }
65
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:
78};
80
81class QItemSelection;
83
84class Q_CORE_EXPORT QItemSelectionModel : public QObject
85{
87 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged
88 BINDABLE bindableModel)
89 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false
90 DESIGNABLE false)
91 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false
92 DESIGNABLE false)
93 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false
94 DESIGNABLE false)
95 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged
96 STORED false DESIGNABLE false)
97
98 Q_DECLARE_PRIVATE(QItemSelectionModel)
99
100public:
101
103 NoUpdate = 0x0000,
104 Clear = 0x0001,
105 Select = 0x0002,
106 Deselect = 0x0004,
107 Toggle = 0x0008,
108 Current = 0x0010,
109 Rows = 0x0020,
110 Columns = 0x0040,
111 SelectCurrent = Select | Current,
112 ToggleCurrent = Toggle | Current,
113 ClearAndSelect = Clear | Select
114 };
115
116 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
117 Q_FLAG(SelectionFlags)
118
119 explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
121 virtual ~QItemSelectionModel();
122
123 QModelIndex currentIndex() const;
124
125 Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
126 Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const;
127 Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const;
128
129 Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const;
130 Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const;
131
132 bool hasSelection() const;
133
134 QModelIndexList selectedIndexes() const;
135 Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const;
136 Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const;
137 const QItemSelection selection() const;
138
139 const QAbstractItemModel *model() const;
141 QBindable<QAbstractItemModel *> bindableModel();
142
144
145public Q_SLOTS:
146 virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
147 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
148 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
149 virtual void clear();
150 virtual void reset();
151
152 void clearSelection();
153 virtual void clearCurrentIndex();
154
156 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
157 void currentChanged(const QModelIndex &current, const QModelIndex &previous);
158 void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
159 void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
161
162protected:
164 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
165
166private:
167 Q_DISABLE_COPY(QItemSelectionModel)
168 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
169 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int))
170 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int))
171 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int))
172 Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint))
173 Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint))
174 Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
175};
176
177Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
178
179// We export each out-of-line method individually to prevent MSVC from
180// exporting the whole QList class.
182{
183public:
185 Q_CORE_EXPORT QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
186
187 // reusing QList::swap() here is OK!
188
189 Q_CORE_EXPORT void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
190 Q_CORE_EXPORT bool contains(const QModelIndex &index) const;
191 Q_CORE_EXPORT QModelIndexList indexes() const;
192 Q_CORE_EXPORT void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
193 Q_CORE_EXPORT static void split(const QItemSelectionRange &range,
196};
197Q_DECLARE_SHARED(QItemSelection)
198
199#ifndef QT_NO_DEBUG_STREAM
200Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
201#endif
202
204
207
208#endif // QITEMSELECTIONMODEL_H
LayoutChangeHint
This enum describes the way the model changes layout.
\inmodule QtCore
Definition qproperty.h:809
\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 operator!=(const QItemSelectionRange &other) const
Returns true if the selection range differs from the other range given; otherwise returns false.
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.
bool operator==(const QItemSelectionRange &other) const
Returns true if the selection range is exactly the same as the other range given; otherwise returns f...
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:90
b clear()
Combined button and popup list for selecting options.
#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 &)
#define QT_DECL_METATYPE_EXTERN(TYPE, EXPORT)
Definition qmetatype.h:1367
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:1219
static void split(QT_FT_Vector *b)
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define QT_REQUIRE_CONFIG(feature)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_FLAG(x)
#define Q_INVOKABLE
#define Q_SLOTS
#define Q_PRIVATE_SLOT(d, signature)
#define Q_SIGNALS
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:145
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:163
view setModel(model)
[17] //! [18]
QSqlQueryModel * model
[16]
QSharedPointer< T > other(t)
[5]
QItemSelection * selection
[0]
selection select(topLeft, bottomRight)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent
virtual HRESULT STDMETHODCALLTYPE Select()=0