Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. View model for nested structure with list (QAbstractListModel, QAbstractItemModel)
Forum Updated to NodeBB v4.3 + New Features

View model for nested structure with list (QAbstractListModel, QAbstractItemModel)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 252 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.
  • A Offline
    A Offline
    asersoft
    wrote on last edited by
    #1

    Hello!
    I have the following structures:

    struct Topic {
      QString name;
      bool    available;
      bool    operator==(const Topic& other) const {
        return name == other.name && available == other.available;
      }
    };
    
    struct Lesson {
      QString      name;
      QString      description;
      QList<Topic> topics;
    
      bool operator==(const Lesson& other) const {
        return name == other.name && description == other.description && topics == other.topics;
      }
    };
    

    In QML I have 2 views, the first one would display lesson, description and would also contain another, nested view, which would display list of topics. Something like this:

    StackLayout {
        Repeater {
            model: LessonModel {
            }
            delegate: LessonDelegate {
                name: model.name
                description: model.description
                // topics: model.topics ??? <- topics is model for inner TopicModel?
            }
        }
    }
    

    I'm not really sure how could I achieve that. Until now, I used QAbstractListModel and it was perfectly fine, but for this example looks like this is not enough. I think that maybe QAbstractItemModel could help me, but I'm not sure how could use that in my case?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chids
      wrote on last edited by
      #2

      hi i have a similiar problem, did you end up solving this?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SuhasKrishanamurthy
        wrote on last edited by
        #3

        Yes, in this case, you need a hierarchical model since a Lesson contains multiple Topics. The QAbstractListModel is not sufficient because it only supports a flat list. Instead, you should use QAbstractItemModel to represent the hierarchical data structure.

        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