fix: correctly emit dataChanged

emit dataChanged for all of our proxyRoles when another roles has changed
This commit is contained in:
Grecko 2017-09-16 19:33:22 +02:00
parent c81dcafc35
commit dc154fd46b
2 changed files with 9 additions and 15 deletions

View File

@ -34,6 +34,7 @@ QQmlSortFilterProxyModel::QQmlSortFilterProxyModel(QObject *parent) :
connect(this, &QAbstractItemModel::rowsRemoved, this, &QQmlSortFilterProxyModel::countChanged);
connect(this, &QAbstractItemModel::modelReset, this, &QQmlSortFilterProxyModel::countChanged);
connect(this, &QAbstractItemModel::layoutChanged, this, &QQmlSortFilterProxyModel::countChanged);
connect(this, &QAbstractItemModel::dataChanged, this, &QQmlSortFilterProxyModel::onDataChanged);
setDynamicSortFilter(true);
}
@ -224,16 +225,6 @@ QVariant QQmlSortFilterProxyModel::sourceData(const QModelIndex &sourceIndex, in
return sourceModel()->data(sourceIndex, role);
}
void QQmlSortFilterProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
{
beginResetModel();
if (this->sourceModel())
disconnect(this->sourceModel(), &QAbstractItemModel::dataChanged, this, &QQmlSortFilterProxyModel::sourceDataChanged);
QSortFilterProxyModel::setSourceModel(sourceModel);
connect(this->sourceModel(), &QAbstractItemModel::dataChanged, this, &QQmlSortFilterProxyModel::sourceDataChanged);
endResetModel();
}
QVariant QQmlSortFilterProxyModel::data(const QModelIndex &index, int role) const
{
return sourceData(mapToSource(index), role);
@ -376,6 +367,7 @@ void QQmlSortFilterProxyModel::resetInternalData()
}
m_roleNames = QSortFilterProxyModel::roleNames();
m_proxyRoleMap.clear();
m_proxyRoleNumbers.clear();
auto roles = m_roleNames.keys();
auto maxIt = std::max_element(roles.cbegin(), roles.cend());
@ -384,6 +376,7 @@ void QQmlSortFilterProxyModel::resetInternalData()
++maxRole;
m_roleNames[maxRole] = proxyRole->name().toUtf8();
m_proxyRoleMap[maxRole] = proxyRole;
m_proxyRoleNumbers.append(maxRole);
connect(proxyRole, &ProxyRole::invalidated, this, &QQmlSortFilterProxyModel::emitProxyRolesChanged, Qt::UniqueConnection);
}
}
@ -433,16 +426,17 @@ void QQmlSortFilterProxyModel::initRoles()
updateRoles();
}
void QQmlSortFilterProxyModel::sourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
void QQmlSortFilterProxyModel::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
{
Q_UNUSED(roles);
Q_EMIT dataChanged(topLeft, bottomRight, m_proxyRoleMap.keys().toVector());
if (!roles.isEmpty() && roles != m_proxyRoleNumbers)
Q_EMIT dataChanged(topLeft, bottomRight, m_proxyRoleNumbers);
}
void QQmlSortFilterProxyModel::emitProxyRolesChanged()
{
invalidate();
Q_EMIT dataChanged(index(0,0), index(rowCount() - 1, columnCount() - 1), m_proxyRoleMap.keys().toVector());
Q_EMIT dataChanged(index(0,0), index(rowCount() - 1, columnCount() - 1), m_proxyRoleNumbers);
}
QVariantMap QQmlSortFilterProxyModel::modelDataMap(const QModelIndex& modelIndex) const

View File

@ -72,7 +72,6 @@ public:
QVariant sourceData(const QModelIndex& sourceIndex, const QString& roleName) const;
QVariant sourceData(const QModelIndex& sourceIndex, int role) const;
void setSourceModel(QAbstractItemModel *sourceModel) override;
QVariant data(const QModelIndex& index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
@ -111,7 +110,7 @@ private Q_SLOTS:
void updateSortRole();
void updateRoles();
void initRoles();
void sourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles);
void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles);
void emitProxyRolesChanged();
private:
@ -142,6 +141,7 @@ private:
bool m_completed = false;
QHash<int, QByteArray> m_roleNames;
QHash<int, ProxyRole*> m_proxyRoleMap;
QVector<int> m_proxyRoleNumbers;
};
}