0
votes

I want to display hierarchical Data with a QML-Listview. This means that I have different Cpp datamodels, that have the following structure:

  • Section 1
    • Subsection 1
    • Item
    • Item
    • Subsection 2
    • Item
  • Section 2

The number of subsections is different in every model, so that I'm looking for a general solution to display that data in a flat list like that:

  • Section 1
  • Subsection 1
  • Item
  • Item ...

I'm using Qt 4.8.2 with Qt Quick 1.1.

What I figured out till now:

  • The QML ListView cannot display hierarchical listmodels
  • There are different solutions to display tree-structured extendable lists with multiple Listviews, but that's too complex for my models
  • I can make my model flat by using a Abstract-Class insert different Object types in a flat list, but that would be a lot of work, cause I had to insert lots of loops

Till now I found one solution provided by blackberry cascades, but I can't use it because I have to run my application on Embedded Linux:

Cascades Vegetables Data Model

At this moment I'm thinking about to write my own ListView with Cpp, but I would really like to avoid that because It will be a lot of work to implement that.

I'm new here at stackoverflow, so please let me know if I have to give you more informations.

Thank's in advance.

Quperman

EDIT 05.08.2014:

Temporary Solution:

Since It seems to be hard work for me to implement my own Cpp TreeView, I found another easier solution:

I created an abstract class AbstractItem with an attribute ItemType (enum), that has the following values:

  • Item
  • Section
  • Subsection
  • ...

What I now do is the following: I inherit my Section and Item classes from my new class AbstractItem. Now I put QList into my QAbstractListModel and appended the items, sections, subsections... I can now provide different data in header and item by implementing the data() function with a switch over ItemType. depending on the ItemType I can do a reinterpret_cast to access the data.

Now I have a flat hierarchy that works for me. Sadly I can't use the class hierarchy, but at this moment it seems to be the fastest solution.

1
The lack of a TreeView is a major reason why I think QtQuick 1 and 2 suck, but anyway, the solution is: write your own TreeView, it is possible - I've written 2 already. - user1095108
How did you write your TreeView? Did you write your own Cpp class or have you used QML to realize this? Thanks for your fast answer! - SGbo
Cpp class, of course, QML is like JSON with some scripting ability added. A specification language mostly. Doing complex tasks in QML is usually not worthwhile. - user1095108
Thank you really much, I will now try to implement it. - SGbo

1 Answers

0
votes

15th May 2015 We got beta version of Qt 5.5. See its features. Take a deep breath... Now you can use TreeView component in QML!

Take a look at snapshot for Qt 5.5 documentation.