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. C++ custom treemodel viewing in QML
Forum Updated to NodeBB v4.3 + New Features

C++ custom treemodel viewing in QML

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 2.4k 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.
  • D Offline
    D Offline
    Dragann
    wrote on last edited by
    #1

    hello folks,

    I have subclassed a QAbstractItemModel to create a custom tree model.

    i would like to be able to show specific data in a QML view according to the parent-child structure.

    for example, the tree holds Groups as top-level items and folders as children. The folders also have children items corresponding to files and the files also have children. So what i would want to see is something like:


    Groupname1
    folder1
    info1, info2, info3

    folder2
    info1, info2, info3

    folder3
    info1, info2, info3


    Groupname2
    folder1
    info1, info2, info3

    folder2
    info1, info2, info3

    folder3
    info1, info2, info3

    and so on.

    the info1-2-3 could be specific data about the folders such as filecount, or ownername.

    i have seen lots of examples that only show simple c++ listmodels but nothing like this.

    It would be possible to hack around it but i am looking for the proper way to do something like this, so if somebody could point me in the right direction, that would be really phucking awesome!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QML currently supports only lists. There is no tree support. It will be added in Qt 5.5.

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dragann
        wrote on last edited by
        #3

        yes i am aware of that, nonetheless people have still managed to pull it off using nested views. So i am looking for a way to do it using just listviews.

        I am sure it must be possible in some way to display specific data from a treemodel using nested views

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You can use model roles to display the nesting inside concrete items, for example. So, pass a "column" role from the model and in the item add indentation based on that value. You can also leverage ListView's sections.

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dragann
            wrote on last edited by
            #5

            would it be possible to post an example? I just picked up model/view programming from the "Advanced QT Programming" book.. so the c++ model part i understand, but the QML part is very new to me.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @
              // Assuming the model has "column" role, "modelText" role and "group" role

              ListView {
              delegate: Rectangle {
              // inside the delegate, you can use named roles from the model
              Row {
              Text { id: t1; text: (column == 0)? modelText : "" }
              Text { id: t2; text: (column == 1)? modelText : "" }
              Text { id: t3; text: (column == 2)? modelText : "" }
              }
              }

              // Optional: sections
              section.property: group
              section.delegate: Text { text: group }
              }
              @

              Of course, this is just a quick draft, it can be improved a lot.

              (Z(:^

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dragann
                wrote on last edited by
                #7

                these roles are confusing me... would this mean that every data item in the tree has these attributes? that would mean that every item would hold data about the group and the column... would this not be redundant?

                it would mean that every item, including file items and everything else would hold data about the group it is in.

                correct me if i am wrong.

                my tree structure looks like this

                @Group1
                Folder1
                File1
                File2
                size
                format
                extension
                etc
                File3
                size
                ...
                ...
                Folder2
                ...
                Group2
                ...
                ...
                ...
                ...
                ...
                ...
                ...
                ...
                ...
                @

                if i have understood correctly, roles are attributes of items. Meaning that every item should have those attributes, meaning that even group items would have group role attributes... even format and size items would contain information about the group they are in. It all seems redundant and just doesnt make sense.

                Maybe i just understand it all wrong but this setup does not have columns.. this data is all just variables that come from different tables from a relational sql database..


                Group1
                folder1
                folder2
                folder3

                Group2
                folder1
                folder2
                ....

                and when you click on a folder,, it should show a new screen like

                Folder1
                file1
                file2
                file3

                Folder2
                file1
                file2

                so if i want to display the above data like this, is your example still valid?

                and thank you very much for taking the time to help me, i really really apreciate it very much.

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  [quote]Maybe i just understand it all wrong but this setup does not have columns[/quote]

                  Correct. As mentioned above, QML currently does not support columns. So in my draft I attempted to fake column support by using roles. I agree it is not a perfect solution. Other I've seen often include creating separate ListViews for all subitems. This is not nice either.

                  I've added "section" properties because you seem to have use for it. I have not used that functionality myself, so perhaps my draft code would not work.

                  [quote]so if i want to display the above data like this, is your example still valid?[/quote]

                  In that case I think you could use standard ListView, standard 1D list, create separate ListView for each "level" of detail. You can switch between those levels using a Loader, or state transitions, or some other way.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Dragann
                    wrote on last edited by
                    #9

                    bq. n that case I think you could use standard ListView, standard 1D list, create separate ListView for each “level” of detail

                    this is also what i was thinking. But the question is: for example, How would i make a listview that show only the top level items of my treemodel so that only the children of the rootitem are shown (the groups).. if i know this i can make different lists that show specific details of certain items just like you said. I would be able to select a folder and make a list that shows all the children of the folder (all the files).

                    oh and thank you again, i cannot say how much i apreciate the help because i always try to figure stuff out completely on my own

                    1 Reply Last reply
                    0

                    • Login

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