Accessing QSortFilterProxyModel from QML
-
wrote on 8 Aug 2021, 00:08 last edited by
I have a Model created in C++, register in main as a singleton, and succesfully in use in a view in QML:
qmlRegisterSingletonType<GatewayModel>( "GatewayModel", 1, 0, "GatewayModelQML", [](QQmlEngine *eng, QJSEngine *js) -> QObject * { Q_UNUSED(js); eng->setObjectOwnership(GatewayModel::instance(), QQmlEngine::ObjectOwnership::CppOwnership); return GatewayModel::instance(); });
I now want my QML code to use a QSortFilterProxyModel sitting in front of my "GatewayModel". How do I insert the proxy inbetween my QML code and my model? (In C++? In QML?? How)
-
I have a Model created in C++, register in main as a singleton, and succesfully in use in a view in QML:
qmlRegisterSingletonType<GatewayModel>( "GatewayModel", 1, 0, "GatewayModelQML", [](QQmlEngine *eng, QJSEngine *js) -> QObject * { Q_UNUSED(js); eng->setObjectOwnership(GatewayModel::instance(), QQmlEngine::ObjectOwnership::CppOwnership); return GatewayModel::instance(); });
I now want my QML code to use a QSortFilterProxyModel sitting in front of my "GatewayModel". How do I insert the proxy inbetween my QML code and my model? (In C++? In QML?? How)
wrote on 8 Aug 2021, 02:30 last edited by@ocgltd I need more context to help you but I think that if you want to filter or sort in QML then it is preferable to use this project: https://github.com/oKcerG/SortFilterProxyModel
-
@ocgltd I need more context to help you but I think that if you want to filter or sort in QML then it is preferable to use this project: https://github.com/oKcerG/SortFilterProxyModel
wrote on 8 Aug 2021, 14:13 last edited by ocgltd 8 Aug 2021, 14:14@eyllanesc I"m not sure why that project is preferred, the QSortFilterProxyModel seems pretty straight forward. I'm just not sure how to instantiate it so it sits between my GatewayModel singleton and QML.
-
wrote on 9 Aug 2021, 17:46 last edited by
I think what you want is to set the source model.
proxyModel.setSourceModel(*instance of GatewayModel*);
And then just declare it in main:
engine.rootContext()->setContextProperty("ProxyModel", instance of ProxyModel);
1/4