Is it possible to assign 2 colors to the alternate ListView items in QML? I want to color 1st list item as black, then 2nd list item as blue, then 3rd item as black, then 4th item as blue and so on...
How to achieve this thing in qml? Please share your thoughts. Thanks.
You can use the index property of the delegate to determine if this delegate element is even or odd and use that to change the color of the delegate.
ListView {
anchors.fill: parent
model: 3
delegate:
Rectangle {
width: 20
height: 30
color: index % 2 == 0 ? "blue" : "black"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With