2424
2525#include < QTimer>
2626
27+ #include " global/log.h"
2728#include " uicomponents/view/modelutils.h"
2829
2930using namespace muse ::uicomponents;
@@ -145,7 +146,7 @@ void SortFilterProxyModel::setAlwaysExcludeIndices(const QList<int>& indices)
145146
146147int SortFilterProxyModel::roleFromRoleName (const QString& roleName) const
147148{
148- return roleNames (). key (roleName.toUtf8 (), INVALID_KEY);
149+ return m_roles. value (roleName.toUtf8 (), INVALID_KEY);
149150}
150151
151152QHash<int , QByteArray> SortFilterProxyModel::roleNames () const
@@ -165,11 +166,16 @@ void SortFilterProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
165166
166167 QSortFilterProxyModel::setSourceModel (sourceModel);
167168
169+ updateRoleMap ();
170+
168171 emit sourceModelRoleNamesChanged ();
169172
170173 if (auto sourceSortFilterModel = qobject_cast<SortFilterProxyModel*>(sourceModel)) {
171174 m_subSourceModelConnection = connect (sourceSortFilterModel, &SortFilterProxyModel::sourceModelRoleNamesChanged,
172- this , &SortFilterProxyModel::sourceModelRoleNamesChanged);
175+ this , [this ] {
176+ updateRoleMap ();
177+ emit sourceModelRoleNamesChanged ();
178+ });
173179 }
174180}
175181
@@ -218,3 +224,12 @@ Sorter* SortFilterProxyModel::currentSorter() const
218224
219225 return nullptr ;
220226}
227+
228+ void SortFilterProxyModel::updateRoleMap ()
229+ {
230+ m_roles.clear ();
231+ for (const auto & [role, roleName] : roleNames ().asKeyValueRange ()) {
232+ const auto [it, didInsert] = m_roles.try_emplace (roleName, role);
233+ DO_ASSERT (didInsert);
234+ }
235+ }
0 commit comments