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. Cannot access model index of ObjectModel inside delegate ?
Qt 6.11 is out! See what's new in the release blog

Cannot access model index of ObjectModel inside delegate ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 2.5k 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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #4

      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...

      JonBJ 1 Reply Last reply
      1
      • DiracsbracketD Diracsbracket

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Diracsbracket

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

        This means that ObjectModel is undefined, not index.

        DiracsbracketD 1 Reply Last reply
        2
        • JonBJ JonB

          @Diracsbracket

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

          This means that ObjectModel is undefined, not index.

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by
          #3

          @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?

          1 Reply Last reply
          0
          • DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #4

            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...

            JonBJ 1 Reply Last reply
            1
            • DiracsbracketD Diracsbracket

              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...

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

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

              something.anythingITypeInIncludingMisspellings ...
              

              is scarily dangerous :)

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved