QAbstractItemModelPrivate: add resetting member

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.7 6.5
Change-Id: I7d1fb983e9bf868c48472624ad945ae158115943
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit 9d8663c18e88cb0b5a65f86cfd7726f3d31e04d6)
bb10
Mitch Curtis 2024-09-24 10:40:37 +08:00
parent 3ba0bb61ad
commit 2ea3abed01
2 changed files with 17 additions and 0 deletions

View File

@ -3400,6 +3400,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());
}
@ -3417,8 +3424,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());
}

View File

@ -45,6 +45,8 @@ public:
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);
@ -115,6 +117,8 @@ public:
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);