Accessing QSortFilterProxyModel from QML
-
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)
-
@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
@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.
-
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);