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. [WIP] C++/QML fully recursive TreeView
QtWS25 Last Chance

[WIP] C++/QML fully recursive TreeView

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.8k 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.
  • M Offline
    M Offline
    mcallegari79
    wrote on last edited by
    #1

    Hi everyone, I thought I had to share this, since I spit blood on it for like a couple of weeks.
    I am slowly porting to QML/Quick my Qt-widget based application which makes large use of QTreeWidget so I definitely needed a TreeView replacement.
    QML doesn't like recursive stuff. On one hand it gives you an extreme flexibility thanks to bindings, JS-like code, etc... On the other hand..."it's a trap !":https://drinksleepbekerri.files.wordpress.com/2014/01/star_wars_its_a_trap.jpg

    Here's what I got so far: http://pasteboard.co/LWYTGSB.png
    Code can be fetched here: https://github.com/mcallegari/qmlplayground

    Basically the idea is to have the data model in C++, using nested QAbstractListModel classes, and expose them to QML via QVariant::fromValue.
    This is convenient for the simple fact that you can use role names, so it results in a readable QML code.

    As a plus, I've added the possibility to have user-defined roles, so if you want to use TableView instead of ListView, you can have all the multicolumn facilities that you prefer.

    The fixed roles (minimum requirements for the tree visualization) are: "label", "hasChildren" and "childrenModel"
    The user roles can be added with TreeModel::setColumnNames, passing a QStringList (see app.cpp)

    TODO:

    • items highlight
    • items signalling

    Feel free to comment and propose paches/improvements if you test the code.
    As I said, I worked on it a lot, and tried a whole bunch of ways...all ended in a tunnel of desperation.
    The solution I've found might not be the best, but at least it works !

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yanner
      wrote on last edited by
      #2

      Hello,

      Thank you for posting this. I am dealing with the same problem of showing a c++ model in QML. I checked your source code but couldn't find the actual model there. Could you kindly add those also in your project?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcallegari79
        wrote on last edited by
        #3

        [quote author="yanner" date="1423045398"]Hello,

        Thank you for posting this. I am dealing with the same problem of showing a c++ model in QML. I checked your source code but couldn't find the actual model there. Could you kindly add those also in your project? [/quote]

        What do you mean by "actual model" ?
        For demo purposes the model is hardcoded (see app.cpp) but obviously it should come from your existing C++ classes.

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yanner
          wrote on last edited by
          #4

          I meant the treemodel.h/.cpp and treemodelitem.h/.cpp files. I can see the use of this model class in the app.cpp but not the files itself.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcallegari79
            wrote on last edited by
            #5

            Ohh, I see it now. I've added the missing files.
            I messed things up with .gitignore.

            Sorry about that, and thanks for spotting

            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