Remove the model pointer from QPersistentModelIndexData

It can be just as well received from the QModelIndex member.

Change-Id: I72f930206ca2afed730009778ded0e56e4e6f278
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
bb10
Lars Knoll 2018-11-26 09:56:42 +01:00
parent 4da0c669d3
commit 0023948f83
3 changed files with 5 additions and 16 deletions

View File

@ -77,7 +77,7 @@ void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
{
Q_ASSERT(data);
Q_ASSERT(data->ref.load() == 0);
QAbstractItemModel *model = const_cast<QAbstractItemModel *>(data->model);
QAbstractItemModel *model = const_cast<QAbstractItemModel *>(data->index.model());
// a valid persistent model index with a null model pointer can only happen if the model was destroyed
if (model) {
QAbstractItemModelPrivate *p = model->d_func();
@ -512,10 +512,8 @@ QAbstractItemModel *QAbstractItemModelPrivate::staticEmptyModel()
void QAbstractItemModelPrivate::invalidatePersistentIndexes()
{
for (QPersistentModelIndexData *data : qAsConst(persistent.indexes)) {
for (QPersistentModelIndexData *data : qAsConst(persistent.indexes))
data->index = QModelIndex();
data->model = 0;
}
persistent.indexes.clear();
}
@ -530,7 +528,6 @@ void QAbstractItemModelPrivate::invalidatePersistentIndex(const QModelIndex &ind
QPersistentModelIndexData *data = *it;
persistent.indexes.erase(it);
data->index = QModelIndex();
data->model = 0;
}
}
@ -863,7 +860,6 @@ void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent,
QPersistentModelIndexData *data = *it;
persistent.indexes.erase(persistent.indexes.constFind(data->index));
data->index = QModelIndex();
data->model = 0;
}
}
@ -958,7 +954,6 @@ void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent,
QPersistentModelIndexData *data = *it;
persistent.indexes.erase(persistent.indexes.constFind(data->index));
data->index = QModelIndex();
data->model = 0;
}
}
@ -3294,8 +3289,6 @@ void QAbstractItemModel::changePersistentIndex(const QModelIndex &from, const QM
data->index = to;
if (to.isValid())
d->persistent.insertMultiAtEnd(to, data);
else
data->model = 0;
}
}
@ -3328,8 +3321,6 @@ void QAbstractItemModel::changePersistentIndexList(const QModelIndexList &from,
data->index = to.at(i);
if (data->index.isValid())
toBeReinserted << data;
else
data->model = 0;
}
}

View File

@ -65,11 +65,10 @@ QT_REQUIRE_CONFIG(itemmodel);
class QPersistentModelIndexData
{
public:
QPersistentModelIndexData() : model(0) {}
QPersistentModelIndexData(const QModelIndex &idx) : index(idx), model(idx.model()) {}
QPersistentModelIndexData() {}
QPersistentModelIndexData(const QModelIndex &idx) : index(idx) {}
QModelIndex index;
QAtomicInt ref;
const QAbstractItemModel *model;
static QPersistentModelIndexData *create(const QModelIndex &index);
static void destroy(QPersistentModelIndexData *data);
};

View File

@ -1252,11 +1252,10 @@ void QDirModelPrivate::restorePersistentIndexes()
for (const SavedPersistent &sp : qAsConst(savedPersistent)) {
QPersistentModelIndexData *data = sp.data;
QModelIndex idx = q->index(sp.path, sp.column);
if (idx != data->index || data->model == 0) {
if (idx != data->index || data->index.model() == nullptr) {
//data->model may be equal to 0 if the model is getting destroyed
persistent.indexes.remove(data->index);
data->index = idx;
data->model = q;
if (idx.isValid())
persistent.indexes.insert(idx, data);
}