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. Am I using `TreeView` right?

Am I using `TreeView` right?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 231 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kaixoo
    wrote last edited by
    #1

    I am following the official example for populating a TreeView or QTreeView from Qt and getting to a result. To do that I am subclassing QAbstractItemModel (which is supposedly a flat table, not a classical tree structure) and copying a lot of custom code from the example, such as TreeItem.

    This approach is fine for one treeview, however it feels a little bit artificial with Qt, and could get quite verbose if an application uses TreeView a lot.

    I wonder whether professional Qt apps also use this method from the example, or whether they do something else? I am thinking of friction2d, an open-source Qt app. They use QTreeView a lot but I searched in their code and found no mention of subclassing QAbstractItemModel anywhere!

    I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.

    I also have another question: I need this TreeView to be interactive. So, for example, you should be able to select certain elements in the tree, be able to right-click them and open menu popups, etc. Are there any examples available that teach how to do this?

    JonBJ jeremy_kJ 2 Replies Last reply
    0
    • K Offline
      K Offline
      kaixoo
      wrote last edited by
      #8

      Nevermind, I was using QAbstractItemModel when I should've used QStandardItemModel. Thanks!

      1 Reply Last reply
      0
      • K kaixoo

        I am following the official example for populating a TreeView or QTreeView from Qt and getting to a result. To do that I am subclassing QAbstractItemModel (which is supposedly a flat table, not a classical tree structure) and copying a lot of custom code from the example, such as TreeItem.

        This approach is fine for one treeview, however it feels a little bit artificial with Qt, and could get quite verbose if an application uses TreeView a lot.

        I wonder whether professional Qt apps also use this method from the example, or whether they do something else? I am thinking of friction2d, an open-source Qt app. They use QTreeView a lot but I searched in their code and found no mention of subclassing QAbstractItemModel anywhere!

        I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.

        I also have another question: I need this TreeView to be interactive. So, for example, you should be able to select certain elements in the tree, be able to right-click them and open menu popups, etc. Are there any examples available that teach how to do this?

        JonBJ Online
        JonBJ Online
        JonB
        wrote last edited by
        #2

        @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

        I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.

        For your usage, have you looked at just using the QTreeWidget Class? That is a QTreeView with a simple model of QVariants built into it. Or, for a QTreeView model start from a QStandardItemModel Class.

        JonBJ 1 Reply Last reply
        0
        • K kaixoo

          I am following the official example for populating a TreeView or QTreeView from Qt and getting to a result. To do that I am subclassing QAbstractItemModel (which is supposedly a flat table, not a classical tree structure) and copying a lot of custom code from the example, such as TreeItem.

          This approach is fine for one treeview, however it feels a little bit artificial with Qt, and could get quite verbose if an application uses TreeView a lot.

          I wonder whether professional Qt apps also use this method from the example, or whether they do something else? I am thinking of friction2d, an open-source Qt app. They use QTreeView a lot but I searched in their code and found no mention of subclassing QAbstractItemModel anywhere!

          I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.

          I also have another question: I need this TreeView to be interactive. So, for example, you should be able to select certain elements in the tree, be able to right-click them and open menu popups, etc. Are there any examples available that teach how to do this?

          jeremy_kJ Online
          jeremy_kJ Online
          jeremy_k
          wrote last edited by
          #3

          @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

          [...] I am subclassing QAbstractItemModel (which is supposedly a flat table, not a classical tree structure)

          This is a misunderstanding. Each cell in the model can have children. For example:

          QModelIndex root = model->index(0, 0, nullptr);
          QModelIndex firstChild = model->index(0, 0, root);
          QModelIndex secondGrandChild = model->index(0, 1, firstChild);
          

          The custom followed by QTreeView, QTreeWidget, and I believe the Quick TreeView is to look for children under column zero in each row.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          K 1 Reply Last reply
          0
          • jeremy_kJ jeremy_k

            @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

            [...] I am subclassing QAbstractItemModel (which is supposedly a flat table, not a classical tree structure)

            This is a misunderstanding. Each cell in the model can have children. For example:

            QModelIndex root = model->index(0, 0, nullptr);
            QModelIndex firstChild = model->index(0, 0, root);
            QModelIndex secondGrandChild = model->index(0, 1, firstChild);
            

            The custom followed by QTreeView, QTreeWidget, and I believe the Quick TreeView is to look for children under column zero in each row.

            K Offline
            K Offline
            kaixoo
            wrote last edited by
            #4

            @jeremy_k I get that. I just think it's a bit odd that I have to implement TreeItem myself.

            I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.

            jeremy_kJ GrecKoG 2 Replies Last reply
            0
            • K kaixoo

              @jeremy_k I get that. I just think it's a bit odd that I have to implement TreeItem myself.

              I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.

              jeremy_kJ Online
              jeremy_kJ Online
              jeremy_k
              wrote last edited by
              #5

              @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

              @jeremy_k I get that. I just think it's a bit odd that I have to implement TreeItem myself.

              That just an example, rather than the only option. QStandardItemModel and QStandardItem can be used without much code.

              #include <QApplication>
              #include <QStandardItem>
              #include <QStandardItemModel>
              #include <QTreeView>
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  QStandardItemModel model;
                  model.appendRow(new QStandardItem("parent"));
                  model.item(0)->appendRow(new QStandardItem("child"));
                  QTreeView view;
                  view.setModel(&model);
                  view.show();
                  return a.exec();
              }
              

              I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.

              I'm having trouble parsing this. Does elements further below mean children of top level items? Is the lack of rendering intentional. or a bug in search of a fix? A minimal working example would help.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              1
              • K kaixoo

                @jeremy_k I get that. I just think it's a bit odd that I have to implement TreeItem myself.

                I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.

                GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote last edited by
                #6

                @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

                I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.

                That's incorrect.

                QRangeModel might be of help to avoid having to write the boilerplate.

                K 1 Reply Last reply
                0
                • GrecKoG GrecKo

                  @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

                  I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.

                  That's incorrect.

                  QRangeModel might be of help to avoid having to write the boilerplate.

                  K Offline
                  K Offline
                  kaixoo
                  wrote last edited by
                  #7

                  @GrecKo QRangeModel might be what I'm looking for!

                  Do you know how custom behaviour can be implemented in a TreeView? I haven't found information online, but it's basically features like right clicking to open a Menu, double clicking to rename, key shortcuts to delete...

                  This is going to be for the Assets dialog of my application, which is basically a list that displays all the files that are imported into the current file. Something like the following:

                  - Compositions
                     [x] Active composition
                     [ ] Composition 1
                     [ ] Composition 2
                     ...
                  - Images
                    - Image1
                    - Image2
                  
                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kaixoo
                    wrote last edited by
                    #8

                    Nevermind, I was using QAbstractItemModel when I should've used QStandardItemModel. Thanks!

                    1 Reply Last reply
                    0
                    • K kaixoo has marked this topic as solved
                    • JonBJ JonB

                      @kaixoo said in Am I using &#x60;TreeView&#x60; right?:

                      I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.

                      For your usage, have you looked at just using the QTreeWidget Class? That is a QTreeView with a simple model of QVariants built into it. Or, for a QTreeView model start from a QStandardItemModel Class.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote last edited by
                      #9

                      @JonB said in Am I using &#x60;TreeView&#x60; right?:

                      Or, for a QTreeView model start from a QStandardItemModel Class.

                      1 Reply Last reply
                      2

                      • Login

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