Solved Whose attribute is the 'index' attribute in the QML example?
-
ListView { id: listView model: 10 delegate: ItemDelegate { text: modelData highlighted: ListView.isCurrentItem onClicked: listView.currentIndex = index } }
-
@mirro it is automatically added to the delegate of all Components that can have a delegate
ListView, Repeater ....etc
-
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
-
@J-Hilk Why is the display of ItemDelegate and ListModel used together problematic?
The following two examples show different effectsimport 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 } } } }