diff options
author | Stephen Kelly <steveire@gmail.com> | 2009-07-31 11:00:40 (GMT) |
---|---|---|
committer | Olivier Goffart <ogoffart@trolltech.com> | 2009-08-28 09:34:24 (GMT) |
commit | 4d197ec0eaeae61499d8ee6dc0e98147800f583e (patch) | |
tree | 606dd9392cc8e3bab799c38b118fc0f2c761a30f /src/corelib | |
parent | 7091ec8cd1ec94fb889230d69fc70d40a9f69b2d (diff) | |
download | Qt-4d197ec0eaeae61499d8ee6dc0e98147800f583e.zip Qt-4d197ec0eaeae61499d8ee6dc0e98147800f583e.tar.gz Qt-4d197ec0eaeae61499d8ee6dc0e98147800f583e.tar.bz2 |
Fix the API for resetting QAbstractItemModels.
This commit deprecates the QAIM::reset() method, and adds beginResetModel()
and endResetModel() methods, addressing Qt issue 247023.
http://www.qtsoftware.com/developer/task-tracker/index_html?method=entry&id=247023
If models and proxies use QAIM::reset() alone, then proxies will
emit modelAboutToBeReset after its source model is reset. This means that mapToSource
will not behave as expected (Will always return an invalid index) in a slot connected
to modelAboutToBeReset.
The usecase for this is maintaining viewstate (which items are selected, expanded)
when the model is reset. See BrowserWidget::modelChanged here:
http://websvn.kde.org/trunk/KDE/kdepim/akonadi/akonadiconsole/browserwidget.cpp?view=markup
Task-number: 247023
Reviewed-by: Olivier Goffart <ogoffart@trolltech.com>
Merge-request: 1072
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/kernel/qabstractitemmodel.cpp | 49 | ||||
-rw-r--r-- | src/corelib/kernel/qabstractitemmodel.h | 3 |
2 files changed, 45 insertions, 7 deletions
diff --git a/src/corelib/kernel/qabstractitemmodel.cpp b/src/corelib/kernel/qabstractitemmodel.cpp index 1c9104a..2175542 100644 --- a/src/corelib/kernel/qabstractitemmodel.cpp +++ b/src/corelib/kernel/qabstractitemmodel.cpp @@ -1339,7 +1339,7 @@ void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent, \endlist - \sa layoutAboutToBeChanged(), dataChanged(), headerDataChanged(), reset(), + \sa layoutAboutToBeChanged(), dataChanged(), headerDataChanged(), modelReset(), changePersistentIndex() */ @@ -2769,7 +2769,25 @@ void QAbstractItemModel::endMoveColumns() /*! Resets the model to its original state in any attached views. - The view to which the model is attached to will be reset as well. + Use beginResetModel() and endResetModel() instead whenever possible. If usng this method, the modelAboutToBeReset signal will + not function as expected through proxy models. + + Use this method only if there is no way to call beginResetModel() before invalidating the model. +*/ +void QAbstractItemModel::reset() +{ + Q_D(QAbstractItemModel); + emit modelAboutToBeReset(); + d->invalidatePersistentIndexes(); + emit modelReset(); +} + +/*! + Begins a model reset operation. + + A reset operation resets the model to its current state in any attached views. + + \note Any views attached to this model will be reset as well. When a model is reset it means that any previous data reported from the model is now invalid and has to be queried for again. This also means that @@ -2779,12 +2797,29 @@ void QAbstractItemModel::endMoveColumns() call this function rather than emit dataChanged() to inform other components when the underlying data source, or its structure, has changed. - \sa modelAboutToBeReset(), modelReset() + You must call this function before resetting any internal data structures in your model + or proxy model. + + \sa modelAboutToBeReset(), modelReset(), endResetModel() + \since 4.6 */ -void QAbstractItemModel::reset() +void QAbstractItemModel::beginResetModel() { - Q_D(QAbstractItemModel); emit modelAboutToBeReset(); +} + +/*! + Completes a model reset operation. + + You must call this function after resetting any internal data structure in your model + or proxy model. + + \sa beginResetModel() + \since 4.6 +*/ +void QAbstractItemModel::endResetModel() +{ + Q_D(QAbstractItemModel); d->invalidatePersistentIndexes(); emit modelReset(); } @@ -3256,7 +3291,7 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti This signal is emitted when reset() is called, before the model's internal state (e.g. persistent model indexes) has been invalidated. - \sa reset(), modelReset() + \sa beginResetModel(), modelReset() */ /*! @@ -3266,7 +3301,7 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti This signal is emitted when reset() is called, after the model's internal state (e.g. persistent model indexes) has been invalidated. - \sa reset(), modelAboutToBeReset() + \sa endResetModel(), modelAboutToBeReset() */ /*! diff --git a/src/corelib/kernel/qabstractitemmodel.h b/src/corelib/kernel/qabstractitemmodel.h index b47e4cb..1ca6cbe 100644 --- a/src/corelib/kernel/qabstractitemmodel.h +++ b/src/corelib/kernel/qabstractitemmodel.h @@ -293,6 +293,9 @@ protected: void reset(); + void beginResetModel(); + void endResetModel(); + void changePersistentIndex(const QModelIndex &from, const QModelIndex &to); void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to); QModelIndexList persistentIndexList() const; |