Create multiple instances of QSortFilterProxyModel for view
-
I have two
listviews
on a singlePage
component. The model for both is coming from a singleQSortFilterProxyModel
. The problem is if I set data for oneListView
, the other one is also changed. This happens as there is a single instance of the model.Will I have to create 2 different instances of the
QSortFilterProxyModel
or there is some other way around?My Code
main.cpp
int main(int argc, char *argv[]) { // Application basic initialization QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QtWebEngine::initialize(); QQuickStyle::setStyle("Default"); FilterModel filterModel; FilterList filterList; // Set contexts for QML engine.rootContext()->setContextProperty("filterModel",&filterModel); engine.rootContext()->setContextProperty("filterList",&filterList); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
filterlist.cpp
#include "filterlist.h" FilterList::FilterList(QObject *parent) : QSortFilterProxyModel(parent) { setSourceModel(&m_filterListModel); } void FilterList::searchByCategory(QString filterSubCategory) { setFilterRole(m_filterListModel.FilterListCategoryRole); this->setFilterCaseSensitivity(Qt::CaseInsensitive); this->setFilterFixedString(filterSubCategory); }
mypage.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 1.3 Page { id : somepageid Column{ Button{ id: btn1 text: "btn a" onClicked: { filterList.searchByCategory("category a") } } Button{ id: btn2 text: "btn b" onClicked: { filterList.searchByCategory("category b") } } } ListView{ id: lv1 model: filterList height: 100 delegate: Row{ Text{ text: name } } } ListView{ id: lv2 anchors.top: lv1.bottom model: filterList height: 100 delegate: Row{ Text{ text: name } } } }
-
Hi,
No there is not. However, you can have only one underlying model shared between your two proxies. Therefore, you should not have an instance of your custom model as member variable of your custom proxy.
-
That's correct.
What you are trying to do with only one model is like having two pilots in one car trying to drive into two different directions at the same time.