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 6.7 6.8
Change-Id: I7d1fb983e9bf868c48472624ad945ae158115943
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
  • Loading branch information
mitchcurtis committed Sep 26, 2024
1 parent 3b9f5c8 commit 9d8663c
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 @@ -3401,7 +3401,14 @@ 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.
}

qCDebug(lcReset) << "beginResetModel called; about to emit modelAboutToBeReset";
d->resetting = true;
emit modelAboutToBeReset(QPrivateSignal());
}

Expand All @@ -3419,9 +3426,15 @@ 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.
}

qCDebug(lcReset) << "endResetModel called; about to emit modelReset";
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 9d8663c

Please sign in to comment.