Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create multiple instances of QSortFilterProxyModel for view

Create multiple instances of QSortFilterProxyModel for view

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qsortfilterproxlistview qml
5 Posts 2 Posters 720 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chilarai
    wrote on last edited by chilarai
    #1

    I have two listviews on a single Page component. The model for both is coming from a single QSortFilterProxyModel. The problem is if I set data for one ListView, 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
                }
            }
        }
    
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 2 Replies Last reply
      2
      • SGaistS SGaist

        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.

        C Offline
        C Offline
        chilarai
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          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.

          C Offline
          C Offline
          chilarai
          wrote on last edited by chilarai
          #4

          @SGaist do you mean that I will have to create 2 proxies for the same model for my respective listviews? And that we cannot anyhow manage from a single proxy?

          I think I get it now

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved