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
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
73
77
78const QPointer<QQuickItem> QQuickHeaderViewBasePrivate::delegateItemAt(int row, int col) const
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{
158 qmlWarning(q_func()) << "Setting syncDirection other than Qt::"
159 << QVariant::fromValue(orientation()).toString()
160 << " is invalid.";
162 }
164}
165
167{
168 // Our proxy model shares no common model items with HeaderView.model. So
169 // selections done in HeaderView cannot be represented in an ItemSelectionModel
170 // that is shared with the syncView (and for the same reason, the mapping functions
171 // modelIndex(cell) and cellAtIndex(index) have not been overridden either).
172 // Instead, we set the internal proxy model as selection source model.
174}
175
178{
180 d->m_headerDataProxyModel.m_headerView = this;
183 d->setOrientation(orient);
184 setSyncDirection(orient);
185}
186
188 : QQuickTableView(dd, parent)
189{
191 d->m_headerDataProxyModel.m_headerView = this;
192}
193
197
199{
200 Q_D(const QQuickHeaderViewBase);
201 return d->m_textRole;
202}
203
205{
207 if (d->m_textRole == role)
208 return;
209
210 d->m_textRole = role;
212}
213
218
225
230
241
243
245{
246 if (m_model == newSourceModel)
247 return;
249 disconnectFromModel();
250 m_model = newSourceModel;
251 connectToModel();
253}
254
259
265
267{
268 return index(row, column);
269}
270
272{
273 if (parent.isValid())
274 return 0;
275 return m_model.isNull() ? -1 : (m_orientation == Qt::Horizontal ? 1 : m_model->rowCount(parent));
276}
277
279{
280 if (parent.isValid())
281 return 0;
282 return m_model.isNull() ? -1 : (m_orientation == Qt::Vertical ? 1 : m_model->columnCount(parent));
283}
284
286{
287 if (m_model.isNull())
288 return QVariant();
289 if (!hasIndex(index.row(), index.column()))
290 return QModelIndex();
291 auto section = m_orientation == Qt::Vertical ? index.row() : index.column();
292 return m_model->headerData(section, m_orientation, role);
293}
294
296{
297 if (!hasIndex(index.row(), index.column()))
298 return false;
299 auto section = m_orientation == Qt::Vertical ? index.row() : index.column();
300 auto ret = m_model->setHeaderData(section, m_orientation, value, role);
301 emit dataChanged(index, index, { role });
302 return ret;
303}
304
306{
307 if (!parent.isValid())
308 return rowCount(parent) > 0 && columnCount(parent) > 0;
309 return false;
310}
311
312QHash<int, QByteArray> QHeaderDataProxyModel::roleNames() const
313{
314 using namespace Qt::Literals::StringLiterals;
315
316 auto names = m_model ? m_model->roleNames() : QAbstractItemModel::roleNames();
317 if (m_headerView) {
318 QString textRole = m_headerView->textRole();
319 if (textRole.isEmpty())
320 textRole = u"display"_s;
321 if (!names.values().contains(textRole.toUtf8().constData())) {
322 qmlWarning(m_headerView).nospace() << "The 'textRole' property contains a role that doesn't exist in the model: "
323 << textRole << ". Check your model's roleNames() implementation";
324 }
325 }
326
327 return names;
328}
329
331{
332 return QVariant::fromValue(static_cast<QObject *>(const_cast<QHeaderDataProxyModel *>(this)));
333}
334
336{
337 if (o == m_orientation)
338 return;
340 m_orientation = o;
342}
343
345{
346 return m_orientation;
347}
348
349QPointer<QAbstractItemModel> QHeaderDataProxyModel::sourceModel() const
350{
351 return m_model;
352}
353
354void QHeaderDataProxyModel::connectToModel()
355{
356 if (m_model.isNull())
357 return;
359 this, [this](Qt::Orientation orient, int first, int last) {
360 if (orient != orientation())
361 return;
362 if (orient == Qt::Horizontal) {
364 } else {
366 }
367 });
400}
401
402void QHeaderDataProxyModel::disconnectFromModel()
403{
404 if (m_model.isNull())
405 return;
406 m_model->disconnect(this);
407}
408
415
419
426
430
432
434
436
438
440
441#include "moc_qquickheaderview_p_p.cpp"
442
443#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.
virtual QHash< int, QByteArray > roleNames() const
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...
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
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}.
QHash< int, QByteArray > roleNames() const override
QQuickHeaderViewBase * m_headerView
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.
static constexpr Policy Preferred
static constexpr Policy Fixed
\inmodule QtCore
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
\inmodule QtCore\reentrant
Definition qpoint.h:25
bool isNull() const noexcept
Definition qpointer.h:84
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)
QAbstractItemModel * selectionSourceModel() override
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:63
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:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QByteArray toUtf8() const &
Definition qstring.h:634
\inmodule QtCore
Definition qvariant.h:65
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:536
Combined button and popup list for selecting options.
Definition qcompare.h:63
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
@ UniqueConnection
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
return ret
const GLfloat * m
GLuint index
[2]
GLint first
GLenum GLenum GLsizei void GLsizei void * column
GLuint GLuint * names
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]