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. Index undefined for elements in ObjectModel?

Index undefined for elements in ObjectModel?

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.7k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    billconan
    wrote on last edited by
    #1

    according to Qt document http://qt-project.org/doc/qt-5/qml-qtqml-models-objectmodel.html#index-prop

    An item can determine its index within the model via the index attached property.

    my object model code:
    @ ObjectModel {
    id: itemModel

        Rectangle {
            width: view.width; height: view.height
            color: "#FFFEF0"
            Text { text: "Page 1"; font.bold: true; anchors.centerIn: parent }
    
            Component.onDestruction: if (printDestruction) print("destroyed 1")
        }
        Rectangle {
            width: view.width; height: view.height
            color: "#F0FFF7"
            Text { text: "Page 2"; font.bold: true; anchors.centerIn: parent }
    
            Component.onDestruction: if (printDestruction) print("destroyed 2")
        }
        Rectangle {
            width: view.width; height: view.height
            color: view.currentIndex == index ? "blue" : "white"
            Text { text: "Page 3"; font.bold: true; anchors.centerIn: parent }
    
            Component.onDestruction: if (printDestruction) print("destroyed 3")
        }@
    

    but when I ran, I saw this:

    qrc:///main.qml:30: ReferenceError: index is not defined

    what's the problem?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Looks like a bug to me. The count property works but not the index.

      157

      1 Reply Last reply
      0
      • B Offline
        B Offline
        billconan
        wrote on last edited by
        #3

        will file a bug.

        thanks!

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Did you try with itemModel.index ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            That too doesn't work.

            157

            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