Important: Please read the Qt Code of Conduct -

QObjectList-based Model in c++

  • I've been looking and experimenting with the MVC framework offered by Qt for a couple days.
    I built some dummy application, sub-classed QStandardItemModel, plugged it in a QListView and added a QDataWidgetMapper and I was able to add/remove items, edit them, reorder them. Great!
    It all works well, although you quickly have to write your own delegates if you want to do something fancy.

    Now I'm trying to map this to a model with data nested on 2 levels, and items having different properties depending on their type.
    In this example: a list of vehicles and house with a parent owner.

    ├─ Adam
    │  ├─ age: 45
    │  ├─ sex: m
    │  ├─ vehicle
    │  │  ├─ color: blue
    │  │  ├─ wheels: 4
    │  │  └─ mileage: 1000
    │  └─ house
    │     └─ location: Denver
    ├─ Betty
    │  ├─ age: 40
    │  ├─ sex: f
    │  ├─ house
    │  │  └─ location: Atlanta
    │  └─ house
    │     └─ location: Charlotte
    └─ Charly
       ├─ age: 20
       ├─ sex: m
       └─ vehicle
          ├─ color: red
          ├─ wheels: 2
          └─ mileage: 50000

    So it seems that the row/column based models don't fit anymore.
    Also a single QDataWidgetMapper will not allow the edition of an owner, a vehicle and a house, since they have different properties.

    Browsing the documentation I found a QML example saying we can "use a QList<QObject*> as a model" exposing the object QProperties as items.
    Is the one and only approach in my use case? Is it possible to do this without QML?

  • Lifetime Qt Champion

    @JulienMaille said in QObjectList-based Model in c++:

    So it seems that the row/column based models don't fit anymore.

    Why not? What you store in the knots is up to you.

Log in to reply