Cannot access model index of ObjectModel inside delegate ?



  • Hi,
    I have an ObjectModel which I dynamically populate with model delegates with Item as root element.

    The documentation states that

    ObjectModel.index : int
    This attached property holds the index of this delegate's item within the model.
    It is attached to each instance of the delegate.

    However I cannot seem to get this index property. I have tried

    Item {
         id: delegateItem
         ...
         MouseArea {
            anchors.fill: parent
    
            onClicked: {
                console.debug("Pressed: " + delegateItem.ObjectModel.index)
           }
        }    
    }
    

    But this gives the error

    TypeError: Cannot read property 'index' of undefined
    

    Of course, I have also tried to access index directly, as I would when using e.g. a ListModel, but that also does not work.

    Furthermore, the following example always returns -1 when the delegate is pressed:

        ObjectModel {
            id: itemModel
    
            Rectangle {
                height: 30; width: 80; color: "red"
                MouseArea {
                    anchors.fill: parent
                    onClicked : console.debug(ObjectModel.index)
                }
            }
    
            Rectangle {
                height: 30; width: 80; color: "green"
                MouseArea {
                    anchors.fill: parent
                    onClicked : console.debug(ObjectModel.index)
                }
            }
    
            Rectangle {
                height: 30; width: 80; color: "blue"
                MouseArea {
                    anchors.fill: parent
                    onClicked : console.debug(ObjectModel.index)
                }
            }
        }
    
        ListView {
            anchors.fill: parent
            model: itemModel
        }
    

    Any ideas?



  • @Diracsbracket

    console.debug("Pressed: " + delegateItem.ObjectModel.index)
    
    TypeError: Cannot read property 'index' of undefined
    

    This means that ObjectModel is undefined, not index.



  • @JonB said in Cannot access model index of ObjectModel inside delegate ?:

    This means that ObjectModel is undefined, not index

    Sure. But how can it be? I invoke the debug statement inside the delegate, upon a click (and therefore the delegate is instantiated). Furthermore, the delegate is appended to the ObjectModel, and the delegate is shown correctly inside the view. But when I click it, it gives the above error. How then should I access that attached property within the delegate?



  • Thanks to @jonB who got me thinking a bit more, I finally got it to work by adding the following import:

    import QtQml.Models 2.10 as QM2
    

    and then using:

    console.debug("Pressed: " + delegateItem.QM2.ObjectModel.index)
    

    Although I never declare an ObjectModel element inside the current QML file scope, I apparently needed to do the import anyway because I try to access the attached property ObjectModel.index. Without the import, ObjectModel is not defined.

    Furthermore, I needed to use some import qualifier (here QM2) because otherwise, Qt Creator underlines the field names of a ListModel I have in the same QML file as having Invalid property name:

    0_1531897676129_dccc22d7-b9e5-4169-a821-e5e61d4ddb5b-image.png

    Note how this qualifier also needs to be used when accessing the attached property delegateItem.QM2.ObjectModel.index...



  • @Diracsbracket
    This is why I find JavaScript allowing you to write

    something.anythingITypeInIncludingMisspellings ...
    

    is scarily dangerous :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.