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. DelegateModel DelegateModelGroup under the hood
Forum Updated to NodeBB v4.3 + New Features

DelegateModel DelegateModelGroup under the hood

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 301 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.
  • R Offline
    R Offline
    Redman
    wrote on last edited by
    #1

    Hello,

    I have a QAbstractListModel subclass in c++ and I want to conditionally render items with help of DelegateModel and DelegateModelGroup.

    The following code works

    model: DelegateModel {
    
                    model: myModel
    
                    // Create filter group error, which is empty in the beginning
                    groups: [
                        DelegateModelGroup {
                            id: errorGroup
                            includeByDefault: false
                            name: 'error'
                        }
                    ]
    
                    // Filter the delegates to only show items that are in the "error" group
                    filterOnGroup: "error"
    
                    Component.onCompleted: {
                        // Items contains all items from the model
                        var itemsGroup = items
                        for (var i = 0; i < itemsGroup.count; i++) {
                            // Get the item at i
                            var item = itemsGroup.get(i)
                            // Access the data and do the conditional addGroups
                            if (condition) {
                                itemsGroup.addGroups(i, 1, ["error"])
                            }
                        }
                    }
    
                    delegate: Rectangle {
                         ...
                }
    

    And following code does not work. And by does not work I mean that when accessing some roles from my model in the delegate the value is undefined and I can not assign it to my qml stuff.

                   Component.onCompleted: {
                        var rowCount = myModel.rowCount()
                        
                        // Remove all items in the default "items" group
                        items.remove(0, rowCount)
                        for (var i = 0; i < rowCount; i++) {
                            var idx = myModel.index(i)
                            if (!idx.valid) {
                                continue
                            }
                            var entry = myModel.data(idx)
                            if (condition) {
                                items.insert(entry, "error")
                            }
                        }
                    }
    

    With the second approach the error group is populated correctly and the delegates accordigly drawn. But there is data missing within the delegate.

    The only way I could explain this is by assuming that by filtering on the items which are in the default items group and putting them in the error groups some kind of binding between DelegateModel and myModel stays intact, while doing the other approach something breaks.

    Anyone knows any specifics?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Redman
      wrote last edited by
      #2

      @kkoehne sorry for tagging you, but do you know anything about that?

      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