Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Implement QSortFilterProxyModel with own TreeModel?
QtWS25 Last Chance

Implement QSortFilterProxyModel with own TreeModel?

Scheduled Pinned Locked Moved Solved General and Desktop
qsortfilterproxtreemodeltreeviewsort
11 Posts 4 Posters 3.1k 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
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 23 Dec 2018, 16:00 last edited by Christian Ehrlicher
    #2

    Go for the QSortFilterProxyModel. An example can be found here: http://doc.qt.io/qt-5/qtwidgets-itemviews-basicsortfiltermodel-example.html or http://doc.qt.io/qt-5/qtwidgets-itemviews-customsortfiltermodel-example.html

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    3
    • O Offline
      O Offline
      Opa114
      wrote on 23 Dec 2018, 20:10 last edited by
      #3

      Thanks for the information. I tried wo work on it a little bit.
      I did a small example like this:

      I implemented the EditableTreeModel form my first post) - the official qt example and then my small code looks like this inside the mainwidow.cpp

      QStringList headers;
      headers << tr("Title") << tr("Description");
      model = new EditableTreeModel(headers, QString("Firtsname\tLastname\t"));
      
      
      QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
      proxyModel->setSourceModel(model);
      treeView->setModel(proxyModel);
      treeView->setSortingEnabled(true);
      

      So it works and sorting, too, if i add more data so that more rows where created but i have a problem, when i try to retrieve the data. I created a function which is called on double click inside a TreeView Item:

      void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
      {
      qDebug() << "on_treeView_doubleClicked called";
      qDebug() << model->data(index, Qt::DisplayRole);
      }
      

      Then i got the following output and error:

      on_treeView_doubleClicked called
      
      Trying to construct an instance of an invalid type, type id: 7890276
      

      So it seems there is a problem with getting the data. So what i'm doing wrong? If i dont use the QSortFilterProxyModel the correct data retrieved.

      M 1 Reply Last reply 23 Dec 2018, 20:14
      0
      • O Opa114
        23 Dec 2018, 20:10

        Thanks for the information. I tried wo work on it a little bit.
        I did a small example like this:

        I implemented the EditableTreeModel form my first post) - the official qt example and then my small code looks like this inside the mainwidow.cpp

        QStringList headers;
        headers << tr("Title") << tr("Description");
        model = new EditableTreeModel(headers, QString("Firtsname\tLastname\t"));
        
        
        QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
        proxyModel->setSourceModel(model);
        treeView->setModel(proxyModel);
        treeView->setSortingEnabled(true);
        

        So it works and sorting, too, if i add more data so that more rows where created but i have a problem, when i try to retrieve the data. I created a function which is called on double click inside a TreeView Item:

        void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
        {
        qDebug() << "on_treeView_doubleClicked called";
        qDebug() << model->data(index, Qt::DisplayRole);
        }
        

        Then i got the following output and error:

        on_treeView_doubleClicked called
        
        Trying to construct an instance of an invalid type, type id: 7890276
        

        So it seems there is a problem with getting the data. So what i'm doing wrong? If i dont use the QSortFilterProxyModel the correct data retrieved.

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 23 Dec 2018, 20:14 last edited by
        #4

        Hi
        I think you need to use
        http://doc.qt.io/qt-5/qsortfilterproxymodel.html#mapToSource
        to look up data in the model.

        O 1 Reply Last reply 23 Dec 2018, 23:31
        2
        • M mrjj
          23 Dec 2018, 20:14

          Hi
          I think you need to use
          http://doc.qt.io/qt-5/qsortfilterproxymodel.html#mapToSource
          to look up data in the model.

          O Offline
          O Offline
          Opa114
          wrote on 23 Dec 2018, 23:31 last edited by
          #5

          @mrjj

          i think this was the problem:

          qDebug() << model->data(index, Qt::DisplayRole);
          

          i tried to get the datafrom the source model and not from the proxy model. After changing the line to getting data() from the proxy model it works and i get the right data. If i try to remove the selected row, the row (and data) will be removed from the underlying source model.

          M 1 Reply Last reply 23 Dec 2018, 23:36
          1
          • O Opa114
            23 Dec 2018, 23:31

            @mrjj

            i think this was the problem:

            qDebug() << model->data(index, Qt::DisplayRole);
            

            i tried to get the datafrom the source model and not from the proxy model. After changing the line to getting data() from the proxy model it works and i get the right data. If i try to remove the selected row, the row (and data) will be removed from the underlying source model.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 23 Dec 2018, 23:36 last edited by
            #6

            @Opa114
            Yes, that also works if you ask the proxy.
            However, the model is unaware of the filtering so thats why
            the given index didnt work with model.

            O 1 Reply Last reply 23 Dec 2018, 23:37
            1
            • M mrjj
              23 Dec 2018, 23:36

              @Opa114
              Yes, that also works if you ask the proxy.
              However, the model is unaware of the filtering so thats why
              the given index didnt work with model.

              O Offline
              O Offline
              Opa114
              wrote on 23 Dec 2018, 23:37 last edited by
              #7

              @mrjj
              yes i understand what the problem is, but if it works directly with the proxy model it is better for me to work with that.

              M 1 Reply Last reply 23 Dec 2018, 23:41
              0
              • O Opa114
                23 Dec 2018, 23:37

                @mrjj
                yes i understand what the problem is, but if it works directly with the proxy model it is better for me to work with that.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 23 Dec 2018, 23:41 last edited by
                #8

                @Opa114
                Hi, nothing wrong working with the proxy :)

                1 Reply Last reply
                1
                • O Offline
                  O Offline
                  Opa114
                  wrote on 24 Dec 2018, 11:21 last edited by Opa114
                  #9

                  @mrjj
                  Now i have another problem. I implemented a onDoubleClick Listener, which should return the data. My Treeview is set to expandAll(), because i have some TreeView entries which have children, like this structure:

                  - Root 1
                      - Child 1
                  - Root 2
                      - Child 2.1
                      - Child 2.2
                  - Root 3
                  

                  When i double click on one of the children my function return everytime the data from Root 1, so when i click on Child 2.2 i got the data from Root 1 and when i click on Child 1 i got the data from Root 1. I think something is worng with the indexes? But how can i solve this?

                  V 1 Reply Last reply 24 Dec 2018, 11:54
                  0
                  • O Opa114
                    24 Dec 2018, 11:21

                    @mrjj
                    Now i have another problem. I implemented a onDoubleClick Listener, which should return the data. My Treeview is set to expandAll(), because i have some TreeView entries which have children, like this structure:

                    - Root 1
                        - Child 1
                    - Root 2
                        - Child 2.1
                        - Child 2.2
                    - Root 3
                    

                    When i double click on one of the children my function return everytime the data from Root 1, so when i click on Child 2.2 i got the data from Root 1 and when i click on Child 1 i got the data from Root 1. I think something is worng with the indexes? But how can i solve this?

                    V Offline
                    V Offline
                    VRonin
                    wrote on 24 Dec 2018, 11:54 last edited by
                    #10

                    @Opa114 said in Implement QSortFilterProxyModel with own TreeModel?:

                    I think something is worng with the indexes?

                    Good guess, can you show us your code?

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    O 1 Reply Last reply 24 Dec 2018, 12:08
                    1
                    • V VRonin
                      24 Dec 2018, 11:54

                      @Opa114 said in Implement QSortFilterProxyModel with own TreeModel?:

                      I think something is worng with the indexes?

                      Good guess, can you show us your code?

                      O Offline
                      O Offline
                      Opa114
                      wrote on 24 Dec 2018, 12:08 last edited by Opa114
                      #11

                      @VRonin said in Implement QSortFilterProxyModel with own TreeModel?:

                      Good guess, can you show us your code?

                      I solved it: I had to pass the parent index (tree structure), so the line should look like this:

                      QVariant data = m_proxy->data(m_proxy->index(index.row(), 4, index.parent()));
                      
                      1 Reply Last reply
                      0

                      11/11

                      24 Dec 2018, 12:08

                      • Login

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