Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set alternate colors for ListView items in QML

Tags:

qt

qml

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.

like image 463
Rajeev Sahu Avatar asked Nov 23 '12 05:11

Rajeev Sahu


1 Answers

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"
    }
}
like image 73
JuliusG Avatar answered Sep 21 '22 11:09

JuliusG