Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickheaderview.cpp
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#include <QtQuickTemplates2/private/qquickheaderview_p_p.h>
5#include <algorithm>
6
68
71{
72}
73
75{
76}
77
79{
80 return loadedTableItem(QPoint(col, row))->item;
81}
82
84{
86 return QVariant::fromValue(model.data());
87#if QT_CONFIG(transposeproxymodel)
88 if (auto model = m_transposeProxyModel.sourceModel())
90#endif
92}
93
94template <typename P, typename M>
95inline bool proxyModelSetter(QQuickHeaderViewBase *const q, P &proxyModel, M *model)
96{
97 if (model) {
98 if (model == proxyModel.sourceModel())
99 return true;
100 proxyModel.setSourceModel(model);
101 const auto &modelVariant = QVariant::fromValue(std::addressof(proxyModel));
102 bool isProxyModelChanged = (modelVariant != QQuickTableViewPrivate::get(q)->QQuickTableViewPrivate::modelImpl());
103 QQuickTableViewPrivate::get(q)->QQuickTableViewPrivate::setModelImpl(modelVariant);
104 //Necessary, since TableView's assigned model not changed, but proxy's source changed
105 if (!isProxyModelChanged)
106 emit q->modelChanged();
107 return true;
108 }
109 proxyModel.setSourceModel(nullptr);
110 return false;
111}
112
114{
117 // Case 1: newModel is QAbstractTableModel
119 return;
120#if QT_CONFIG(transposeproxymodel)
121 // Case 2: newModel is QAbstractItemModel but not QAbstractTableModel
123 && proxyModelSetter(q, m_transposeProxyModel, newModel.value<QAbstractItemModel *>()))
124 return;
125#endif
126
128}
129
131{
133
135 auto newModel = assignedSyncView->model();
136 if (auto m = newModel.value<QAbstractItemModel *>())
138 }
139
141
142 isTransposed = false;
143 const auto aim = model->abstractItemModel();
144 if (orientation() == Qt::Horizontal) {
145 // For models that are just a list or a number, and especially not a
146 // table, we transpose the view when the orientation is horizontal.
147 // The model (list) will then be laid out horizontally rather than
148 // vertically, which is the otherwise the default.
149 isTransposed = !aim || aim->columnCount() == 1;
150 }
151 if (m_textRole.isEmpty() && aim)
152 m_textRole = QLatin1String("display");
153}
154
156{
159 qmlWarning(q_func()) << "Setting syncDirection other than Qt::"
160 << QVariant::fromValue(orientation()).toString()
161 << " is invalid.";
163 }
164 if (assignedSyncView) {
165 QBoolBlocker fixupGuard(inUpdateContentSize, true);
166 if (orientation() == Qt::Horizontal) {
167 q->setLeftMargin(assignedSyncView->leftMargin());
168 q->setRightMargin(assignedSyncView->rightMargin());
169 } else {
170 q->setTopMargin(assignedSyncView->topMargin());
171 q->setBottomMargin(assignedSyncView->bottomMargin());
172 }
173 }
175}
176
179{
180 d_func()->setOrientation(orient);
181 setSyncDirection(orient);
182}
183
186{
187}
188
190{
191}
192
194{
195 Q_D(const QQuickHeaderViewBase);
196 return d->m_textRole;
197}
198
200{
202 if (d->m_textRole == role)
203 return;
204
205 d->m_textRole = role;
207}
208
210{
212}
213
215{
217 return;
219}
220
223{
224}
225
234{
235}
236
238
240{
241 if (m_model == newSourceModel)
242 return;
244 disconnectFromModel();
245 m_model = newSourceModel;
246 connectToModel();
248}
249
251{
253}
254
256{
258 return QModelIndex();
259}
260
262{
263 return index(row, column);
264}
265
267{
268 if (parent.isValid())
269 return 0;
270 return m_model.isNull() ? -1 : (m_orientation == Qt::Horizontal ? 1 : m_model->rowCount(parent));
271}
272
274{
275 if (parent.isValid())
276 return 0;
277 return m_model.isNull() ? -1 : (m_orientation == Qt::Vertical ? 1 : m_model->columnCount(parent));
278}
279
281{
282 if (m_model.isNull())
283 return QVariant();
284 if (!hasIndex(index.row(), index.column()))
285 return QModelIndex();
286 auto section = m_orientation == Qt::Vertical ? index.row() : index.column();
287 return m_model->headerData(section, m_orientation, role);
288}
289
291{
292 if (!hasIndex(index.row(), index.column()))
293 return false;
294 auto section = m_orientation == Qt::Vertical ? index.row() : index.column();
295 auto ret = m_model->setHeaderData(section, m_orientation, value, role);
296 emit dataChanged(index, index, { role });
297 return ret;
298}
299
301{
302 if (!parent.isValid())
303 return rowCount(parent) > 0 && columnCount(parent) > 0;
304 return false;
305}
306
308{
309 return QVariant::fromValue(static_cast<QObject *>(const_cast<QHeaderDataProxyModel *>(this)));
310}
311
313{
314 if (o == m_orientation)
315 return;
317 m_orientation = o;
319}
320
322{
323 return m_orientation;
324}
325
327{
328 return m_model;
329}
330
331void QHeaderDataProxyModel::connectToModel()
332{
333 if (m_model.isNull())
334 return;
336 this, [this](Qt::Orientation orient, int first, int last) {
337 if (orient != orientation())
338 return;
339 if (orient == Qt::Horizontal) {
341 } else {
343 }
344 });
377}
378
379void QHeaderDataProxyModel::disconnectFromModel()
380{
381 if (m_model.isNull())
382 return;
383 m_model->disconnect(this);
384}
385
387 : QQuickHeaderViewBase(Qt::Horizontal, parent)
388{
391}
392
394{
395}
396
398 : QQuickHeaderViewBase(Qt::Vertical, parent)
399{
401 setResizableRows(true);
402}
403
405{
406}
407
409
411
413
415
417
418#include "moc_qquickheaderview_p_p.cpp"
419
420#include "moc_qquickheaderview_p.cpp"
void rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal)
Q_INVOKABLE int const QModelIndex & parent
Returns the parent of the model item with the given index.
void endResetModel()
Completes a model reset operation.
void modelAboutToBeReset(QPrivateSignal)
void columnsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after columns have been removed from the model.
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before rows are inserted into the model.
virtual Q_INVOKABLE QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data for the given role and section in the header with the specified orientation.
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before columns are inserted into the model.
Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns {true} if the model returns a valid QModelIndex for row and column with parent,...
void modelReset(QPrivateSignal)
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles=QList< int >())
This signal is emitted whenever the data in an existing item changes.
virtual Q_INVOKABLE int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
void columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal)
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before columns are removed from the model.
void layoutChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before rows are removed from the model.
void beginResetModel()
Begins a model reset operation.
void rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal)
virtual Q_INVOKABLE int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
void rowsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after rows have been inserted into the model.
void columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal)
QModelIndex createIndex(int row, int column, const void *data=nullptr) const
Creates a model index for the given row and column with the internal pointer ptr.
void columnsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after columns have been inserted into the model.
void rowsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after rows have been removed from the model.
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
Sets the data for the given role and section in the header with the specified orientation to the valu...
QHeaderDataProxyModel is a proxy AbstractItemModel type that maps source model's headerData() to corr...
Qt::Orientation orientation() const
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
Returns the sibling at row and column for the item at index, or an invalid QModelIndex if there is no...
QAbstractItemModel * sourceModel
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
QVariant variantValue() const
QHeaderDataProxyModel(QObject *parent=nullptr)
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Returns {true} if parent has any children; otherwise returns {false}.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the role data for the item at index to value.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
void setOrientation(Qt::Orientation o)
void setSourceModel(QAbstractItemModel *newSourceModel)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
\inmodule QtCore
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3099
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore
Definition qpointer.h:18
bool isNull() const
Returns true if the referenced object has been destroyed or if there is no referenced object; otherwi...
Definition qpointer.h:67
virtual const QAbstractItemModel * abstractItemModel() const
void setFlickableDirection(FlickableDirection)
QVariant modelImpl() const override
void setModelImpl(const QVariant &newModel) override
QQuickHeaderViewBasePrivate()
\qmltype HorizontalHeaderView \inqmlmodule QtQuick.Controls\inherits TableView
Qt::Orientation orientation() const
const QPointer< QQuickItem > delegateItemAt(int row, int col) const
QHeaderDataProxyModel m_headerDataProxyModel
void setOrientation(Qt::Orientation orientation)
QQuickHeaderViewBase(Qt::Orientation orient, QQuickItem *parent=nullptr)
friend class QQuickHorizontalHeaderView
friend class QQuickVerticalHeaderView
void setTextRole(const QString &role)
QPointer< QQuickItem > item
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
virtual QVariant modelImpl() const
FxTableItem * loadedTableItem(const QPoint &cell) const
QQmlInstanceModel * model
static QQuickTableViewPrivate * get(QQuickTableView *q)
QPointer< QQuickTableView > assignedSyncView
Qt::Orientations assignedSyncDirection
virtual void setModelImpl(const QVariant &newModel)
void setResizableColumns(bool enabled)
void setResizableRows(bool enabled)
void setSyncDirection(Qt::Orientations direction)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\inmodule QtCore
Definition qvariant.h:64
T value() const &
Definition qvariant.h:511
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:531
Combined button and popup list for selecting options.
Orientation
Definition qnamespace.h:97
@ Horizontal
Definition qnamespace.h:98
@ Vertical
Definition qnamespace.h:99
@ UniqueConnection
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
return ret
const GLfloat * m
GLuint index
[2]
GLint first
GLenum GLenum GLsizei void GLsizei void * column
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
bool proxyModelSetter(QQuickHeaderViewBase *const q, P &proxyModel, M *model)
#define M(_x, _y)
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define emit
#define Q_UNUSED(x)
QSqlQueryModel * model
[16]
QLayoutItem * child
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent