Skip to content

Commit

Permalink
QAbstractItemModelPrivate: add resetting member
Browse files Browse the repository at this point in the history
This allows QQmlDelegateModel to know if a QAbstractItemModel subclass
is in the process of a reset, which it can't know if beginResetModel
was called in the model's constructor.

As an added bonus, it also allows us to warn the user if they call
endResetModel with a previous call to beginResetModel.

Task-number: QTBUG-125053
Task-number: QTBUG-127340
Pick-to: 6.5
Change-Id: I7d1fb983e9bf868c48472624ad945ae158115943
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit 9d8663c)
(cherry picked from commit 2ea3abe)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
  • Loading branch information
mitchcurtis authored and Qt Cherry-pick Bot committed Sep 30, 2024
1 parent aa70762 commit 8d811bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/corelib/itemmodels/qabstractitemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,13 @@ void QAbstractItemModel::endMoveColumns()
*/
void QAbstractItemModel::beginResetModel()
{
Q_D(QAbstractItemModel);
if (d->resetting) {
qWarning() << "beginResetModel called on" << this << "without calling endResetModel first";
// Warn, but don't return early in case user code relies on the incorrect behavior.
}

d->resetting = true;
emit modelAboutToBeReset(QPrivateSignal());
}

Expand All @@ -3412,8 +3419,14 @@ void QAbstractItemModel::beginResetModel()
void QAbstractItemModel::endResetModel()
{
Q_D(QAbstractItemModel);
if (!d->resetting) {
qWarning() << "endResetModel called on" << this << "without calling beginResetModel first";
// Warn, but don't return early in case user code relies on the incorrect behavior.
}

d->invalidatePersistentIndexes();
resetInternalData();
d->resetting = false;
emit modelReset(QPrivateSignal());
}

Expand Down
4 changes: 4 additions & 0 deletions src/corelib/itemmodels/qabstractitemmodel_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate
QAbstractItemModelPrivate();
~QAbstractItemModelPrivate();

static const QAbstractItemModelPrivate *get(const QAbstractItemModel *model) { return model->d_func(); }

void removePersistentIndexData(QPersistentModelIndexData *data);
void movePersistentIndexes(const QList<QPersistentModelIndexData *> &indexes, int change, const QModelIndex &parent,
Qt::Orientation orientation);
Expand Down Expand Up @@ -115,6 +117,8 @@ class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate
void insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data);
} persistent;

bool resetting = false;

static const QHash<int,QByteArray> &defaultRoleNames();
static bool isVariantLessThan(const QVariant &left, const QVariant &right,
Qt::CaseSensitivity cs = Qt::CaseSensitive, bool isLocaleAware = false);
Expand Down

0 comments on commit 8d811bb

Please sign in to comment.