Nominate our 2022 Qt Champions!

Can Qt Qml DelegateModel items have different indexes in multiple groups?

  • After following the tutorial on how to sort ListViews in Qml using DelegateModels I encountered a crash bug in Qt caused by the way Qml updates its components QTBUG-53677. It seems that removing model items from the "unsorted" group causes the crash reliably.

    In an attempt to work around this I tried to leave the items in the "unsorted" group as well as adding them to the default "items" group and moving them. After quite a bit of debugging it seems that the call to move causes the index to update for all groups that the item belongs to. Is this the expected behaviour or is this a bug in Qt? DelegateModels seem much less powerful if they have to share an index across DelegateModelGroups.

    I made a simple example in Qt Creator that illustrates the problem.

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQml.Models 2.1
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        ListModel {
            id: dataModel
            ListElement {
                name: "Polly"
            ListElement {
                name: "Penny"
        DelegateModel {
            id: visualModel
            groups: [
                VisualDataGroup {
                    id: unsortedItems
                    name: "unsorted"
                    includeByDefault: true
                VisualDataGroup {
                    id: sortedItems
                    name: "sorted"
                    includeByDefault: true
        Component.onCompleted: {
            var item = unsortedItems.get(1)
            console.assert(item.unsortedIndex === 1, "item.unsortedIndex was initialised incorrectly")
            console.assert(item.sortedIndex === 1, "item.sortedIndex was initialised incorrectly")
            // only expecting this to change item.sortedIndex
            sortedItems.move(1, 0)
            console.log(item.unsortedIndex, item.sortedIndex)
            console.assert(item.unsortedIndex === 1, "item.unsortedIndex has also been modified.")
            console.assert(item.sortedIndex === 0, "item.sortedIndex is incorrect.")
            model: dataModel
            delegate: Text {
                text: name
        Rectangle {
            anchors.fill: parent
            ListView {
                anchors.fill: parent
                model: visualModel

    The original problem was seen in a large c++ application using Qt 5.5.1. The simple example was tested in Qt Creator with Qt 5.6.1.

    The documentation on ModelDelegateGroup is a bit thin so it may be that I have the syntax wrong. I tried debugging the c++ implementation (qtdeclarative\src\qml\types\qqmldelegatemodel.cpp) but couldn't see how the data structure was storing the group information.

    I previously asked this on stackoverflow but got no answers so I'm reposting here.


Log in to reply