[WIP] C++/QML fully recursive TreeView
-
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.jpgHere's what I got so far: http://pasteboard.co/LWYTGSB.png
Code can be fetched here: https://github.com/mcallegari/qmlplaygroundBasically 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 ! -
[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. -
Ohh, I see it now. I've added the missing files.
I messed things up with .gitignore.Sorry about that, and thanks for spotting