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
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{
61
63 if (auto columnCount = q->columnCount(); columnCount > 0)
64 emit q->headerDataChanged(Qt::Horizontal, 0, columnCount - 1);
65 }
66
68 if (auto rowCount = q->rowCount(); rowCount > 0)
69 emit q->headerDataChanged(Qt::Vertical, 0, rowCount - 1);
70 }
71
74}
75
77{
78 const bool isUpdateScheduled = updateHorizontalHeader || updateVerticalHeader;
79
80 if (orientation == Qt::Horizontal && !updateHorizontalHeader)
82 else if (orientation == Qt::Vertical && !updateVerticalHeader)
84 else
85 return;
86
87 if (!isUpdateScheduled) {
90 }
91}
92
94{
95 if (parent.isValid())
96 return;
98}
99
101{
102 if (parent.isValid())
103 return;
106}
107
109{
110 if (parent.isValid())
111 return;
112 if (model->rowCount() == 0)
114}
115
117{
118 if (parent.isValid())
119 return;
121}
122
124{
125 if (parent.isValid())
126 return;
129}
130
132{
133 if (parent.isValid())
134 return;
135 if (model->columnCount() == 0)
137}
138
148
158
166
175{
177 d->model.removeBindingUnlessInWrapper();
178 // Special case to handle nullptr models. Otherwise we will have unwanted
179 // notifications.
180 const QAbstractItemModel *currentModel = d->model.valueBypassingBindings();
182 return;
183 static const struct {
184 const char *signalName;
185 const char *slotName;
186 } connectionTable[] = {
187 // clang-format off
188 { SIGNAL(destroyed()), SLOT(_q_sourceModelDestroyed()) },
189 { SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), SLOT(_q_sourceModelRowsAboutToBeInserted(QModelIndex,int,int)) },
190 { SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(_q_sourceModelRowsInserted(QModelIndex,int,int)) },
191 { SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(_q_sourceModelRowsRemoved(QModelIndex,int,int)) },
192 { SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), SLOT(_q_sourceModelColumnsAboutToBeInserted(QModelIndex,int,int)) },
193 { SIGNAL(columnsInserted(QModelIndex,int,int)), SLOT(_q_sourceModelColumnsInserted(QModelIndex,int,int)) },
194 { SIGNAL(columnsRemoved(QModelIndex,int,int)), SLOT(_q_sourceModelColumnsRemoved(QModelIndex,int,int)) }
195 // clang-format on
196 };
197
198 if (sourceModel != currentModel) {
199 if (currentModel) {
200 for (const auto &c : connectionTable)
201 disconnect(currentModel, c.signalName, this, c.slotName);
202 }
203
204 if (sourceModel) {
205 d->model.setValueBypassingBindings(sourceModel);
206 for (const auto &c : connectionTable)
207 connect(sourceModel, c.signalName, this, c.slotName);
208 } else {
209 d->model.setValueBypassingBindings(QAbstractItemModelPrivate::staticEmptyModel());
210 }
211 d->model.notify();
212 }
213}
214
219{
220 Q_D(const QAbstractProxyModel);
222 return nullptr;
223 return d->model;
224}
225
226QBindable<QAbstractItemModel *> QAbstractProxyModel::bindableSourceModel()
227{
229 return QBindable<QAbstractItemModel *>(&d->model);
230}
231
236{
238 return d->model->submit();
239}
240
245{
247 d->model->revert();
248}
249
250
275{
276 QModelIndexList proxyIndexes = proxySelection.indexes();
277 QItemSelection sourceSelection;
278 for (int i = 0; i < proxyIndexes.size(); ++i) {
279 const QModelIndex proxyIdx = mapToSource(proxyIndexes.at(i));
280 if (!proxyIdx.isValid())
281 continue;
282 sourceSelection << QItemSelectionRange(proxyIdx);
283 }
284 return sourceSelection;
285}
286
293{
294 QModelIndexList sourceIndexes = sourceSelection.indexes();
295 QItemSelection proxySelection;
296 for (int i = 0; i < sourceIndexes.size(); ++i) {
297 const QModelIndex srcIdx = mapFromSource(sourceIndexes.at(i));
298 if (!srcIdx.isValid())
299 continue;
300 proxySelection << QItemSelectionRange(srcIdx);
301 }
302 return proxySelection;
303}
304
308QVariant QAbstractProxyModel::data(const QModelIndex &proxyIndex, int role) const
309{
310 Q_D(const QAbstractProxyModel);
311 return d->model->data(mapToSource(proxyIndex), role);
312}
313
317QVariant QAbstractProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
318{
319 Q_D(const QAbstractProxyModel);
320 int sourceSection = section;
321 if (orientation == Qt::Horizontal) {
322 if (rowCount() > 0) {
323 const QModelIndex proxyIndex = index(0, section);
324 sourceSection = mapToSource(proxyIndex).column();
325 }
326 } else {
327 if (columnCount() > 0) {
328 const QModelIndex proxyIndex = index(section, 0);
329 sourceSection = mapToSource(proxyIndex).row();
330 }
331 }
332 return d->model->headerData(sourceSection, orientation, role);
333}
334
338QMap<int, QVariant> QAbstractProxyModel::itemData(const QModelIndex &proxyIndex) const
339{
340 Q_D(const QAbstractProxyModel);
341 return d->model->itemData(mapToSource(proxyIndex));
342}
343
348{
349 Q_D(const QAbstractProxyModel);
350 return d->model->flags(mapToSource(index));
351}
352
357{
359 return d->model->setData(mapToSource(index), value, role);
360}
361
365bool QAbstractProxyModel::setItemData(const QModelIndex &index, const QMap< int, QVariant >& roles)
366{
368 return d->model->setItemData(mapToSource(index), roles);
369}
370
374bool QAbstractProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
375{
377 int sourceSection;
378 if (orientation == Qt::Horizontal) {
379 const QModelIndex proxyIndex = index(0, section);
380 sourceSection = mapToSource(proxyIndex).column();
381 } else {
382 const QModelIndex proxyIndex = index(section, 0);
383 sourceSection = mapToSource(proxyIndex).row();
384 }
385 return d->model->setHeaderData(sourceSection, orientation, value, role);
386}
387
393{
395 return d->model->clearItemData(mapToSource(index));
396}
397
402{
403 Q_D(const QAbstractProxyModel);
404 return mapFromSource(d->model->buddy(mapToSource(index)));
405}
406
411{
412 Q_D(const QAbstractProxyModel);
413 return d->model->canFetchMore(mapToSource(parent));
414}
415
420{
422 d->model->fetchMore(mapToSource(parent));
423}
424
429{
431 d->model->sort(column, order);
432}
433
438{
439 Q_D(const QAbstractProxyModel);
440 return d->model->span(mapToSource(index));
441}
442
447{
448 Q_D(const QAbstractProxyModel);
449 return d->model->hasChildren(mapToSource(parent));
450}
451
456{
457 return index(row, column, idx.parent());
458}
459
464{
465 Q_D(const QAbstractProxyModel);
467 list.reserve(indexes.size());
468 for (const QModelIndex &index : indexes)
470 return d->model->mimeData(list);
471}
472
474 int *sourceRow, int *sourceColumn, QModelIndex *sourceParent) const
475{
476 Q_Q(const QAbstractProxyModel);
477 *sourceRow = -1;
478 *sourceColumn = -1;
479 if (row == -1 && column == -1) {
480 *sourceParent = q->mapToSource(parent);
481 } else if (row == q->rowCount(parent)) {
482 *sourceParent = q->mapToSource(parent);
483 *sourceRow = model->rowCount(*sourceParent);
484 } else {
485 QModelIndex proxyIndex = q->index(row, column, parent);
486 QModelIndex sourceIndex = q->mapToSource(proxyIndex);
487 *sourceRow = sourceIndex.row();
488 *sourceColumn = sourceIndex.column();
489 *sourceParent = sourceIndex.parent();
490 }
491}
492
498 int row, int column, const QModelIndex &parent) const
499{
500 Q_D(const QAbstractProxyModel);
501 int sourceDestinationRow;
502 int sourceDestinationColumn;
503 QModelIndex sourceParent;
504 d->mapDropCoordinatesToSource(row, column, parent, &sourceDestinationRow, &sourceDestinationColumn, &sourceParent);
505 return d->model->canDropMimeData(data, action, sourceDestinationRow, sourceDestinationColumn, sourceParent);
506}
507
513 int row, int column, const QModelIndex &parent)
514{
516 int sourceDestinationRow;
517 int sourceDestinationColumn;
518 QModelIndex sourceParent;
519 d->mapDropCoordinatesToSource(row, column, parent, &sourceDestinationRow, &sourceDestinationColumn, &sourceParent);
520 return d->model->dropMimeData(data, action, sourceDestinationRow, sourceDestinationColumn, sourceParent);
521}
522
527{
528 Q_D(const QAbstractProxyModel);
529 return d->model->mimeTypes();
530}
531
536{
537 Q_D(const QAbstractProxyModel);
538 return d->model->supportedDragActions();
539}
540
545{
546 Q_D(const QAbstractProxyModel);
547 return d->model->supportedDropActions();
548}
549
553QHash<int,QByteArray> QAbstractProxyModel::roleNames() const
554{
555 Q_D(const QAbstractProxyModel);
556 return d->model->roleNames();
557}
558
573QModelIndex QAbstractProxyModel::createSourceIndex(int row, int col, void *internalPtr) const
574{
575 if (sourceModel())
576 return sourceModel()->createIndex(row, col, internalPtr);
577 return QModelIndex();
578}
579
581
582#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.
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 scheduleHeaderUpdate(Qt::Orientation orientation)
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
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void reserve(qsizetype size)
Definition qlist.h:753
\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 const QAbstractItemModel * model() const noexcept
Returns a pointer to the model containing the item that this index refers to.
QObject * parent
Definition qobject.h:73
\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
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
If the database supports returning the size of a query (see QSqlDriver::hasFeature()),...
int columnCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
\inmodule QtCore
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
SortOrder
Definition qnamespace.h:121
DropAction
@ QueuedConnection
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint index
[2]
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...