From 9f8b448cf45d71bb28ec4b9dd9300282a2b07d55 Mon Sep 17 00:00:00 2001 From: Volker Krause Date: Thu, 19 Nov 2015 16:17:29 +0100 Subject: [PATCH] Fix resetting or changing the source model of a KRecursiveFilterProxyModel. We need to disconnect from a previous source model if present, not the new one, and we need to be prepared for the new source model to be 0. REVIEW: 126117 --- src/krecursivefilterproxymodel.cpp | 38 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/krecursivefilterproxymodel.cpp b/src/krecursivefilterproxymodel.cpp index 8219ef4..dbb6eba 100644 --- a/src/krecursivefilterproxymodel.cpp +++ b/src/krecursivefilterproxymodel.cpp @@ -336,26 +336,28 @@ bool KRecursiveFilterProxyModel::acceptRow(int sourceRow, const QModelIndex &sou void KRecursiveFilterProxyModel::setSourceModel(QAbstractItemModel *model) { - // Standard disconnect. - if (passRolesToDataChanged()) { - disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), - this, SLOT(sourceDataChanged(QModelIndex,QModelIndex,QVector))); - } else { - disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - this, SLOT(sourceDataChanged(QModelIndex,QModelIndex))); - } + // Standard disconnect of the previous source model, if present + if (sourceModel()) { + if (passRolesToDataChanged()) { + disconnect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), + this, SLOT(sourceDataChanged(QModelIndex,QModelIndex,QVector))); + } else { + disconnect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), + this, SLOT(sourceDataChanged(QModelIndex,QModelIndex))); + } - disconnect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), - this, SLOT(sourceRowsAboutToBeInserted(QModelIndex,int,int))); + disconnect(sourceModel(), SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), + this, SLOT(sourceRowsAboutToBeInserted(QModelIndex,int,int))); - disconnect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), - this, SLOT(sourceRowsInserted(QModelIndex,int,int))); + disconnect(sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), + this, SLOT(sourceRowsInserted(QModelIndex,int,int))); - disconnect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), - this, SLOT(sourceRowsAboutToBeRemoved(QModelIndex,int,int))); + disconnect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), + this, SLOT(sourceRowsAboutToBeRemoved(QModelIndex,int,int))); - disconnect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), - this, SLOT(sourceRowsRemoved(QModelIndex,int,int))); + disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)), + this, SLOT(sourceRowsRemoved(QModelIndex,int,int))); + } QSortFilterProxyModel::setSourceModel(model); @@ -423,6 +425,10 @@ void KRecursiveFilterProxyModel::setSourceModel(QAbstractItemModel *model) // to see if H matches the filter (which it now does as L now exists). // That is done in sourceRowsInserted. + if (!model) { + return; + } + if (passRolesToDataChanged()) { disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QVector)));