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. Whose attribute is the 'index' attribute in the QML example?
Forum Updated to NodeBB v4.3 + New Features

Whose attribute is the 'index' attribute in the QML example?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 271 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by
    #1
    ListView {
        id: listView
        model: 10
        delegate: ItemDelegate {
            text: modelData
            highlighted: ListView.isCurrentItem
            onClicked: listView.currentIndex = index
        }
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • M mirro
      ListView {
          id: listView
          model: 10
          delegate: ItemDelegate {
              text: modelData
              highlighted: ListView.isCurrentItem
              onClicked: listView.currentIndex = index
          }
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @mirro it is automatically added to the delegate of all Components that can have a delegate

      ListView, Repeater ....etc


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      M 1 Reply Last reply
      1
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        That's a context property given by the view when it instantiates the delegate.

        Note that this feature is meant to be replaced by required properties in newer code : https://www.qt.io/blog/new-qml-language-features-in-qt-5.15

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @mirro it is automatically added to the delegate of all Components that can have a delegate

          ListView, Repeater ....etc

          M Offline
          M Offline
          mirro
          wrote on last edited by
          #4

          @J-Hilk Why is the display of ItemDelegate and ListModel used together problematic?
          The following two examples show different effects

          import QtQuick.Window 2.2
          import QtQuick.Controls 2.2
          
          ListModel {
              id:contactModel
              ListElement {
                  name: "Bill Smith"
                  number: "555 3264"
              }
              ListElement {
                  name: "John Brown"
                  number: "555 8426"
              }
              ListElement {
                  name: "Sam Wise"
                  number: "555 0473"
              }
          }
          
          Window {
              visible: true
              width: 300
              height: 450
              title: qsTr("Hello World")
          
              ListView{
                  id:listView
                  anchors.fill: parent
                  model:contactModel
                  snapMode: ListView.SnapOneItem
                  orientation:ListView.Horizontal
                  delegate: ItemDelegate {
                            Rectangle{
                            
                                          width: listView.width
                            
                                          height: listView.height
                            
                                          color: index%2 ? "red":"yellow"
                            
                                          Label{
                            
                                              anchors.centerIn: parent
                            
                                              font.pointSize: 100
                            
                                              text: index
                            
                                          }
                            
                                    }
                         }
              }
          }
          
          import QtQuick.Window 2.2
          import QtQuick.Controls 2.2
          
          Window {
              visible: true
              width: 300
              height: 450
              title: qsTr("Hello World")
          
              ListView{
                  id:listView
                  anchors.fill: parent
                  model:10
                  snapMode: ListView.SnapOneItem
                  orientation:ListView.Horizontal
                  delegate: Rectangle{
                        width: listView.width
                        height: listView.height
                        color: index%2 ? "red":"yellow"
                        Label{
                            anchors.centerIn: parent
                            font.pointSize: 100
                            text: index
                        }
                  }
              }
          }
          
          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