fix: workaround for ListModel starting with no roles

A ListModel with no roles first won't emit modelReset when its roles
change (QTBUG-57971).

Fixes #26
This commit is contained in:
Pierre-Yves Siret 2017-06-07 02:10:22 +02:00
parent 7c72171c7a
commit ac45b581d6

View File

@ -147,7 +147,6 @@ void QQmlSortFilterProxyModel::componentComplete()
invalidate();
sort(0);
}
QVariant QQmlSortFilterProxyModel::sourceData(const QModelIndex& sourceIndex, const QString& roleName) const
{
int role = sourceModel()->roleNames().key(roleName.toUtf8());
@ -244,8 +243,11 @@ bool QQmlSortFilterProxyModel::lessThan(const QModelIndex& source_left, const QM
void QQmlSortFilterProxyModel::resetInternalData()
{
QSortFilterProxyModel::resetInternalData();
if (roleNames().isEmpty()) // workaround for when a model has no roles and roles are added when the model is populated (ListModel)
if (sourceModel() && QSortFilterProxyModel::roleNames().isEmpty()) { // workaround for when a model has no roles and roles are added when the model is populated (ListModel)
// QTBUG-57971
connect(sourceModel(), &QAbstractItemModel::rowsInserted, this, &QQmlSortFilterProxyModel::initRoles);
connect(this, &QAbstractItemModel::rowsAboutToBeInserted, this, &QQmlSortFilterProxyModel::initRoles);
}
}
void QQmlSortFilterProxyModel::invalidateFilter()
@ -287,8 +289,10 @@ void QQmlSortFilterProxyModel::updateRoles()
void QQmlSortFilterProxyModel::initRoles()
{
disconnect(sourceModel(), &QAbstractItemModel::rowsInserted, this, &QQmlSortFilterProxyModel::initRoles);
disconnect(this, &QAbstractItemModel::rowsAboutToBeInserted, this , &QQmlSortFilterProxyModel::initRoles);
resetInternalData();
updateRoles();
}
QVariantMap QQmlSortFilterProxyModel::modelDataMap(const QModelIndex& modelIndex) const