Jan Win
Jan Win

Reputation: 147

QML - Retrieving index of delegate in DelegateModel

I have trouble retrieving the index of a delegate that is instantiated inside a DelegateModel for a ListView. The minimal example as following:

LastProcedures.qml

ListModel {
ListElement {
    procedure: "Liver Resection"
    surgeon: "Prof. Dr. Joyride"
    recent: true
    }
    ...
}

main.qml

ListView {
        id: list_lastProcedures
        model: displayDelegateModel
    }
DelegateModel {
    id: displayDelegateModel
    delegate: lastProceduresDelegate
    model: LastProcedures {}
    groups: [
                DelegateModelGroup {
                    includeByDefault: false
                    name: "recent"
                }
            ]
    filterOnGroup: "recent"
    Component.onCompleted: {
        var rowCount = model.count;
        items.remove(0,rowCount);
        for( var i = 0;i < rowCount;i++ ) {
            var entry = model.get(i);
                // Only the recent three
                if((entry.recent == true) && (items.count < 3)) {
                    items.insert(entry, "recent");
                }
            }
        }
    }
Component {
    id: lastProceduresDelegate
    Text{
        text: model.index
    }
}

The text index prints always -1. Without a DelegateModel it prints the index in the ListView. How can I access the correct index of the delegate in the Listview?

Upvotes: 0

Views: 3356

Answers (3)

Amfasis
Amfasis

Reputation: 4208

The DelegateModel has some hidden magic regarding groups (it's not very visible but it's here ). For each group you create, the DelegateModel attached property will receive two new properties: <group>Index and in<Group>.

In your case this means you will get the following properties: recentIndex and inRecent (or in your own answer: recenttrueIndex and inRecenttrue).

I think with what you want to do you should go the recenttrue route and draft the Component as follows:

Component {
    id: lastProceduresDelegate
    Text {
        text: lastProceduresDelegate.DelegateModel.recenttrueIndex
    }
}

Upvotes: 0

Liubo
Liubo

Reputation: 41

you can use "lastProceduresDelegate.DelegateModel.itemsIndex" instead of "model.index"

just like this:

Component {
    id: lastProceduresDelegate
    Text{
        text: lastProceduresDelegate.DelegateModel.itemsIndex
    }

Upvotes: 4

Jan Win
Jan Win

Reputation: 147

I ended up with not removing all entries and adding them back to groups, but instead just remove unwanted entries. This ways the index stays valid. If someone could explain this behavior further, that would be nice.

DelegateModel {
    id: displayDelegateModel
    delegate: lastProceduresDelegate
    model: LastProcedures {}
    groups: [
                DelegateModelGroup {
                    includeByDefault: true
                    name: "recenttrue"
                }
            ]
    filterOnGroup: "recenttrue"
    Component.onCompleted: {
        for( var i = 0;i < items.count;i++ ) {
            var entry = items.get(i).model;
            // Only the recent
            if((entry.recent != true)) {
                items.removeGroups(i, 1, "recenttrue");
            }
        }
    }
}

Upvotes: 1

Related Questions