Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qabstractproxymodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
6#include <private/qabstractproxymodel_p.h>
7#include <QtCore/QSize>
8#include <QtCore/QStringList>
9#include <QtCore/QMap>
10
11
13
51//detects the deletion of the source model
52void QAbstractProxyModelPrivate::_q_sourceModelDestroyed()
53{
56}
57
59 Qt::Orientation orientation,
60 int count)
61{
62 return [=](){ emit model->headerDataChanged(orientation, 0, count); };
63}
64
66{
67 if (parent.isValid())
68 return;
70}
71
73{
74 if (parent.isValid())
75 return;
78 const int columnCount = q->columnCount();
79 if (columnCount > 0)
81 }
82}
83
84
86{
87 if (parent.isValid())
88 return;
89 if (model->rowCount() == 0) {
91 const int columnCount = q->columnCount();
92 if (columnCount > 0)
94 }
95}
96
98{
99 if (parent.isValid())
100 return;
102}
103
105{
106 if (parent.isValid())
107 return;
110 const int rowCount = q->rowCount();
111 if (rowCount > 0)
113 }
114}
115
117{
118 if (parent.isValid())
119 return;
120 if (model->columnCount() == 0) {
122 const int rowCount = q->rowCount();
123 if (rowCount > 0)
125 }
126}
127
134{
136}
137
144{
146}
147
152{
153
154}
155
164{
166 d->model.removeBindingUnlessInWrapper();
167 // Special case to handle nullptr models. Otherwise we will have unwanted
168 // notifications.
170 return;
171 static const struct {
172 const char *signalName;
173 const char *slotName;
174 } connectionTable[] = {
175 // clang-format off
176 { SIGNAL(destroyed()), SLOT(_q_sourceModelDestroyed()) },
177 { SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), SLOT(_q_sourceModelRowsAboutToBeInserted(QModelIndex,int,int)) },
178 { SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(_q_sourceModelRowsInserted(QModelIndex,int,int)) },
179 { SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(_q_sourceModelRowsRemoved(QModelIndex,int,int)) },
180 { SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), SLOT(_q_sourceModelColumnsAboutToBeInserted(QModelIndex,int,int)) },
181 { SIGNAL(columnsInserted(QModelIndex,int,int)), SLOT(_q_sourceModelColumnsInserted(QModelIndex,int,int)) },
182 { SIGNAL(columnsRemoved(QModelIndex,int,int)), SLOT(_q_sourceModelColumnsRemoved(QModelIndex,int,int)) }
183 // clang-format on
184 };
185
186 if (sourceModel != d->model) {
187 if (d->model) {
188 for (const auto &c : connectionTable)
189 disconnect(d->model, c.signalName, this, c.slotName);
190 }
191
192 if (sourceModel) {
193 d->model.setValueBypassingBindings(sourceModel);
194 for (const auto &c : connectionTable)
195 connect(d->model, c.signalName, this, c.slotName);
196 } else {
197 d->model.setValueBypassingBindings(QAbstractItemModelPrivate::staticEmptyModel());
198 }
199 d->model.notify();
200 }
201}
202
207{
208 Q_D(const QAbstractProxyModel);
210 return nullptr;
211 return d->model;
212}
213
215{
217 return QBindable<QAbstractItemModel *>(&d->model);
218}
219
224{
226 return d->model->submit();
227}
228
233{
235 d->model->revert();
236}
237
238
263{
264 QModelIndexList proxyIndexes = proxySelection.indexes();
265 QItemSelection sourceSelection;
266 for (int i = 0; i < proxyIndexes.size(); ++i) {
267 const QModelIndex proxyIdx = mapToSource(proxyIndexes.at(i));
268 if (!proxyIdx.isValid())
269 continue;
270 sourceSelection << QItemSelectionRange(proxyIdx);
271 }
272 return sourceSelection;
273}
274
281{
282 QModelIndexList sourceIndexes = sourceSelection.indexes();
283 QItemSelection proxySelection;
284 for (int i = 0; i < sourceIndexes.size(); ++i) {
285 const QModelIndex srcIdx = mapFromSource(sourceIndexes.at(i));
286 if (!srcIdx.isValid())
287 continue;
288 proxySelection << QItemSelectionRange(srcIdx);
289 }
290 return proxySelection;
291}
292
296QVariant QAbstractProxyModel::data(const QModelIndex &proxyIndex, int role) const
297{
298 Q_D(const QAbstractProxyModel);
299 return d->model->data(mapToSource(proxyIndex), role);
300}
301
305QVariant QAbstractProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
306{
307 Q_D(const QAbstractProxyModel);
308 int sourceSection = section;
309 if (orientation == Qt::Horizontal) {
310 if (rowCount() > 0) {
311 const QModelIndex proxyIndex = index(0, section);
312 sourceSection = mapToSource(proxyIndex).column();
313 }
314 } else {
315 if (columnCount() > 0) {
316 const QModelIndex proxyIndex = index(section, 0);
317 sourceSection = mapToSource(proxyIndex).row();
318 }
319 }
320 return d->model->headerData(sourceSection, orientation, role);
321}
322
327{
328 Q_D(const QAbstractProxyModel);
329 return d->model->itemData(mapToSource(proxyIndex));
330}
331
336{
337 Q_D(const QAbstractProxyModel);
338 return d->model->flags(mapToSource(index));
339}
340
345{
347 return d->model->setData(mapToSource(index), value, role);
348}
349
354{
356 return d->model->setItemData(mapToSource(index), roles);
357}
358
362bool QAbstractProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
363{
365 int sourceSection;
366 if (orientation == Qt::Horizontal) {
367 const QModelIndex proxyIndex = index(0, section);
368 sourceSection = mapToSource(proxyIndex).column();
369 } else {
370 const QModelIndex proxyIndex = index(section, 0);
371 sourceSection = mapToSource(proxyIndex).row();
372 }
373 return d->model->setHeaderData(sourceSection, orientation, value, role);
374}
375
381{
383 return d->model->clearItemData(mapToSource(index));
384}
385
390{
391 Q_D(const QAbstractProxyModel);
392 return mapFromSource(d->model->buddy(mapToSource(index)));
393}
394
399{
400 Q_D(const QAbstractProxyModel);
401 return d->model->canFetchMore(mapToSource(parent));
402}
403
408{
410 d->model->fetchMore(mapToSource(parent));
411}
412
417{
419 d->model->sort(column, order);
420}
421
426{
427 Q_D(const QAbstractProxyModel);
428 return d->model->span(mapToSource(index));
429}
430
435{
436 Q_D(const QAbstractProxyModel);
437 return d->model->hasChildren(mapToSource(parent));
438}
439
444{
445 return index(row, column, idx.parent());
446}
447
452{
453 Q_D(const QAbstractProxyModel);
455 list.reserve(indexes.size());
456 for (const QModelIndex &index : indexes)
458 return d->model->mimeData(list);
459}
460
462 int *sourceRow, int *sourceColumn, QModelIndex *sourceParent) const
463{
464 Q_Q(const QAbstractProxyModel);
465 *sourceRow = -1;
466 *sourceColumn = -1;
467 if (row == -1 && column == -1) {
468 *sourceParent = q->mapToSource(parent);
469 } else if (row == q->rowCount(parent)) {
470 *sourceParent = q->mapToSource(parent);
471 *sourceRow = model->rowCount(*sourceParent);
472 } else {
473 QModelIndex proxyIndex = q->index(row, column, parent);
474 QModelIndex sourceIndex = q->mapToSource(proxyIndex);
475 *sourceRow = sourceIndex.row();
476 *sourceColumn = sourceIndex.column();
477 *sourceParent = sourceIndex.parent();
478 }
479}
480
486 int row, int column, const QModelIndex &parent) const
487{
488 Q_D(const QAbstractProxyModel);
489 int sourceDestinationRow;
490 int sourceDestinationColumn;
491 QModelIndex sourceParent;
492 d->mapDropCoordinatesToSource(row, column, parent, &sourceDestinationRow, &sourceDestinationColumn, &sourceParent);
493 return d->model->canDropMimeData(data, action, sourceDestinationRow, sourceDestinationColumn, sourceParent);
494}
495
501 int row, int column, const QModelIndex &parent)
502{
504 int sourceDestinationRow;
505 int sourceDestinationColumn;
506 QModelIndex sourceParent;
507 d->mapDropCoordinatesToSource(row, column, parent, &sourceDestinationRow, &sourceDestinationColumn, &sourceParent);
508 return d->model->dropMimeData(data, action, sourceDestinationRow, sourceDestinationColumn, sourceParent);
509}
510
515{
516 Q_D(const QAbstractProxyModel);
517 return d->model->mimeTypes();
518}
519
524{
525 Q_D(const QAbstractProxyModel);
526 return d->model->supportedDragActions();
527}
528
533{
534 Q_D(const QAbstractProxyModel);
535 return d->model->supportedDropActions();
536}
537
542{
543 Q_D(const QAbstractProxyModel);
544 return d->model->roleNames();
545}
546
561QModelIndex QAbstractProxyModel::createSourceIndex(int row, int col, void *internalPtr) const
562{
563 if (sourceModel())
564 return sourceModel()->createIndex(row, col, internalPtr);
565 return QModelIndex();
566}
567
569
570#include "moc_qabstractproxymodel.cpp"
static QAbstractItemModel * staticEmptyModel()
Q_INVOKABLE int const QModelIndex & parent
Returns the parent of the model item with the given index.
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.
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before columns are inserted into the model.
virtual Q_INVOKABLE int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
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.
friend class QAbstractProxyModel
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.
void _q_sourceModelColumnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
void _q_sourceModelColumnsRemoved(const QModelIndex &parent, int first, int last)
void _q_sourceModelRowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
void mapDropCoordinatesToSource(int row, int column, const QModelIndex &parent, int *source_row, int *source_column, QModelIndex *source_parent) const
void _q_sourceModelRowsRemoved(const QModelIndex &parent, int first, int last)
void _q_sourceModelColumnsInserted(const QModelIndex &parent, int first, int last)
void _q_sourceModelRowsInserted(const QModelIndex &parent, int first, int last)
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting,...
QModelIndex buddy(const QModelIndex &index) const override
\reimp
void fetchMore(const QModelIndex &parent) override
\reimp
Qt::DropActions supportedDragActions() const override
\reimp
~QAbstractProxyModel()
Destroys the proxy model.
QSize span(const QModelIndex &index) const override
\reimp
QVariant data(const QModelIndex &proxyIndex, int role=Qt::DisplayRole) const override
\reimp
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
\reimp
QHash< int, QByteArray > roleNames() const override
\reimp
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
\reimp
virtual Q_INVOKABLE QItemSelection mapSelectionFromSource(const QItemSelection &selection) const
Returns a proxy selection mapped from the specified sourceSelection.
virtual Q_INVOKABLE QModelIndex mapToSource(const QModelIndex &proxyIndex) const =0
Reimplement this function to return the model index in the source model that corresponds to the proxy...
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
virtual Q_INVOKABLE QModelIndex mapFromSource(const QModelIndex &sourceIndex) const =0
Reimplement this function to return the model index in the proxy model that corresponds to the source...
QModelIndex createSourceIndex(int row, int col, void *internalPtr) const
Equivalent to calling createIndex on the source model.
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles) override
\reimp
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
\reimp
bool canFetchMore(const QModelIndex &parent) const override
\reimp
QAbstractItemModel * sourceModel
the source model of this proxy model.
bool clearItemData(const QModelIndex &index) override
\reimp
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
\reimp
QMimeData * mimeData(const QModelIndexList &indexes) const override
\reimp
QMap< int, QVariant > itemData(const QModelIndex &index) const override
\reimp
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
\reimp
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
\reimp
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
\reimp
void revert() override
\reimp
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole) override
\reimp
virtual Q_INVOKABLE QItemSelection mapSelectionToSource(const QItemSelection &selection) const
Returns a source selection mapped from the specified proxySelection.
Qt::DropActions supportedDropActions() const override
\reimp
virtual void setSourceModel(QAbstractItemModel *sourceModel)
Sets the given sourceModel to be processed by the proxy model.
QBindable< QAbstractItemModel * > bindableSourceModel()
QStringList mimeTypes() const override
\reimp
bool submit() override
\reimp
\inmodule QtCore
Definition qproperty.h:809
\inmodule QtCore
Definition qhash.h:818
\inmodule QtCore
Q_CORE_EXPORT QModelIndexList indexes() const
Returns a list of model indexes that correspond to the selected items.
qsizetype size() const noexcept
Definition qlist.h:386
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
void reserve(qsizetype size)
Definition qlist.h:746
Definition qmap.h:186
\inmodule QtCore
Definition qmimedata.h:16
\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.
constexpr int column() const noexcept
Returns the column this model index refers to.
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
QObject * parent
Definition qobject.h:61
\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
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
\inmodule QtCore
Definition qsize.h:25
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
\inmodule QtCore
\inmodule QtCore
Definition qvariant.h:64
Combined button and popup list for selecting options.
Orientation
Definition qnamespace.h:97
@ Horizontal
Definition qnamespace.h:98
@ Vertical
Definition qnamespace.h:99
SortOrder
Definition qnamespace.h:120
DropAction
@ QueuedConnection
static auto emitHeaderDataChanged(QAbstractItemModel *model, Qt::Orientation orientation, int count)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLuint index
[2]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei void GLsizei void * column
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
GLfixed GLfixed GLint GLint order
#define emit
QSqlQueryModel * model
[16]
QList< int > list
[14]
myObject disconnect()
[26]
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent