Skip to content

Commit

Permalink
sortproxymodel: check if source model is a proxy model
Browse files Browse the repository at this point in the history
if the source model is a proxy model, the source model's source
model might be changed and our sortproxymodel won't get notified.
This might happen when we try to pass a QML-based proxy model as
source model, like KItemModel.KSortFilterProxyModel for example.
  • Loading branch information
BLumia committed Jul 25, 2024
1 parent e3fd152 commit 1601a15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions qt/model_view/sortProxyModel/sortproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ void SortProxyModel::setSourceModel(QAbstractItemModel *model)
connect(model, &QAbstractItemModel::dataChanged, this, &SortProxyModel::handleDataChanged);
connect(model, &QAbstractItemModel::rowsInserted, this, &SortProxyModel::handleRowsInserted);
connect(model, &QAbstractItemModel::rowsRemoved, this, &SortProxyModel::handleRowsRemoved);
connect(model, &QAbstractItemModel::modelReset, this, &SortProxyModel::handleModelReset);
}
endResetModel();
}
Expand Down Expand Up @@ -554,6 +555,23 @@ void SortProxyModel::handleRowsRemoved(const QModelIndex &parent, int firstRemov
buildReverseMap(m_proxyToSourceMap, m_sourceToProxyMap);
}

void SortProxyModel::handleModelReset()
{
if (!sourceModel())
return;

const int sourceModelRowCount = sourceModel()->rowCount();
if (sourceModelRowCount > 0)
{
if (sourceModelRowCount != rowCount())
resetInternalData();

handleDataChanged(sourceModel()->index(0, 0),
sourceModel()->index(sourceModel()->rowCount() - 1, 0),
QList<int>());
}
}

/**
* @brief SortProxyModel::isInvalidedRow
* @param row
Expand Down
1 change: 1 addition & 0 deletions qt/model_view/sortProxyModel/sortproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected Q_SLOTS:
void handleDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
void handleRowsInserted(const QModelIndex &parent, int firstNewRow, int lastNewRow);
void handleRowsRemoved(const QModelIndex &parent, int firstRemovedRow, int lastRemovedRow);
void handleModelReset();

bool isInvalidedRow(const int row) const;

Expand Down

0 comments on commit 1601a15

Please sign in to comment.