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 717 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 7 Aug 2020, 12:05 last edited by chilarai 8 Jul 2020, 12:07
    #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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Aug 2020, 12:15 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 7 Aug 2020, 12:29
      2
      • S SGaist
        7 Aug 2020, 12:15

        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 7 Aug 2020, 12:29 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • S SGaist
          7 Aug 2020, 12:15

          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 7 Aug 2020, 13:24 last edited by chilarai 8 Jul 2020, 13:27
          #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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 7 Aug 2020, 18:06 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

            4/5

            7 Aug 2020, 13:24

            • Login

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