August 2, 2010

mario mario
Lab Rat
240 posts

Alternating background color for ListViews

 

Is it possible to have alternating background color for Quick ListViews?

Well, I know that I can, from my delegate, check if the index is even or odd and set background color accordingly. But doing this will mess up the highlight since the highlight is drawn behind the delegate.

I’ve also tried using different z-values for the “background” rectangle in the delegate, the text in the delegate and the highlight but w/o success, any clues?

Of course, this is only relevant if you want an animated highlight else there is no problems doing this in the delegate.

11 replies

August 2, 2010

jochen jochen
Lab Rat
2 posts

If QAbstractItemView::setAlternatingRowColors() is set to true, Qt should do everything for you. Is there a special reason why you can’t use that method? (In the Designer you find a CheckBox named “alternatingRowColors”).

August 2, 2010

Yash Yash
Lab Rat
77 posts

You can achieve this by two ways

  • First already described. i.e use direct available functions
  • Second use CSS (QSS) .Search doc for more info. If can’t let me know.
 Signature 

Chilli CSS Editor - http://csseditor.in (in progress)

August 2, 2010

Bradley Bradley
Lab Rat
314 posts

Setting the z value of the highlight appears to work with the latest Qt (from Fri Jul 30, sha dc63643). In the code below, setting the highlight z to 10 puts it above the other ListView items. It would be nice to position the highlight above the background and below the text, but I don’t have an easy solution for that yet.

list snapshot

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 200
  5.     height: 200
  6.     color: "#bebebe"
  7.  
  8.     Component {
  9.         id: highlight
  10.         Rectangle {
  11.             width: list.width-1
  12.             height: list.currentItem.height-1
  13.             color: "lightsteelblue"
  14.             border.color: "black"
  15.             radius: 5
  16.             opacity: 0.5
  17.             z: 10
  18.             y: list.currentItem.y
  19.             Behavior on y {
  20.                 SpringAnimation {
  21.                     spring: 3
  22.                     damping: 0.2
  23.                 }
  24.             }
  25.         }
  26.     }
  27.  
  28.     ListView {
  29.         id: list
  30.         anchors.left: parent.left
  31.         anchors.right: parent.right
  32.         anchors.top: parent.top
  33.         anchors.bottom: parent.bottom
  34.         model: itemModel
  35.         delegate:  itemDelegate
  36.         highlight: highlight
  37.         highlightFollowsCurrentItem: false
  38.         focus: true
  39.     }
  40.  
  41.     Component {
  42.         id: itemDelegate
  43.         Item {
  44.             id: item
  45.             width: parent.width - 15
  46.             height:  row.height
  47.             function altColor(i) {
  48.                 var colors = [ "#bebebe", "#b7b7b7" ];
  49.                 return colors[i];
  50.             }
  51.             Rectangle {
  52.                 id: background
  53.                 width:  parent.width + 15
  54.                 height: parent.height
  55.                 color: altColor(index%2)
  56.             }
  57.             Row {
  58.                 id: row
  59.                 Item {
  60.                     width: 5
  61.                     height: 1
  62.                 }
  63.                 Text {
  64.                     id: itemText
  65.                     text: model.item
  66.                     font.bold: ListView.isCurrentItem
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     ListModel {
  73.         id: itemModel
  74.         ListElement {
  75.             item: "item1"
  76.         }
  77.         ListElement {
  78.             item: "item2"
  79.         }
  80.         ListElement {
  81.             item: "item3"
  82.         }
  83.         ListElement {
  84.             item: "item4"
  85.         }
  86.         ListElement {
  87.             item: "item5"
  88.         }
  89.     }
  90.  
  91. }

 Signature 

Nokia Certified Qt Specialist.

August 2, 2010

Yash Yash
Lab Rat
77 posts

See how easy Qs you ask everbody know. Please don’t take it otherwise.

 Signature 

Chilli CSS Editor - http://csseditor.in (in progress)

August 2, 2010

mario mario
Lab Rat
240 posts

@jochen and Yash: I’m talking about Qt Quick ListViews and not traditional QtGui ListViews. They aren’t the same.

@bradley: Yes, that’s exactly what I also found out later on… I just realized that setting the opacity can be used as a “workaround” but I would prefer that the ListView has alternating background components built-in or that the ‘z’-value can be use.

August 3, 2010

mbrasser mbrasser
Ant Farmer
452 posts

The highlight (default z-value 0) and delegate (default z-value 1) are essentially siblings, so manipulating the z property will allow you to place one on top of the other, but not interleave them (which is something we originally wanted to support with the API). Hopefully we’ll be able to make some improvements to highlight/delegate interaction in future versions.

Here’s a (rather ugly) way you could achieve a highlight between the background and text today:

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 200
  5.     height: 200
  6.  
  7.     ListView {
  8.         id: view
  9.         focus: true
  10.         anchors.fill: parent
  11.         model: 10
  12.         highlightMoveDuration: 500
  13.         highlightMoveSpeed: -1
  14.         delegate: Item {
  15.             id: wrapper
  16.             width: 200
  17.             height: label.height
  18.             Rectangle {
  19.                 id: backgroundRect
  20.                 y: wrapper.y
  21.                 width: 200
  22.                 height: label.height
  23.                 color: index % 2 ? "green" : "blue"
  24.             }
  25.             Text { id: label; text: index }
  26.             Component.onCompleted: backgroundRect.parent = view.contentItem
  27.         }
  28.         highlight: Rectangle {
  29.             color: "red"
  30.             z: .5
  31.         }
  32.     }
  33. }

August 3, 2010

mario mario
Lab Rat
240 posts

@mbasser: What version of Qt are you using? I can’t make it work on the beta 1 release. Well, maybe it’s time to update to beta 2 anyway.

I’m not sure if there exits a contentItem property on the view (see line 26), I get the following error when running the example (it still shows the listview though):

file://….qml:26: Error: Cannot assign [undefined] to QDeclarativeItem*

I don’t think I have the same result as you, my list view alternates between the following background colors: blue, white, green and not only blue and green. The highlight is only visible when the background is white and I think this is because the background is actually transparent (no rectangle is drawn?). This can be due to I’m only using beta 1 instead of 2.

August 3, 2010

mario mario
Lab Rat
240 posts
Yash wrote:
See how easy Qs you ask everbody know. Please don't take it otherwise.

I’m not sure I understand what you mean Yash?

August 3, 2010

Yash Yash
Lab Rat
77 posts

yea.. My mistake :)

I should read your post bit more.

 Signature 

Chilli CSS Editor - http://csseditor.in (in progress)

August 3, 2010

mario mario
Lab Rat
240 posts

@Yash: You are not the first that miss out that this is a Quick category question :)

August 4, 2010

mbrasser mbrasser
Ant Farmer
452 posts
mario wrote:
@mbasser: What version of Qt are you using? I can't make it work on the beta 1 release. Well, maybe it's time to update to beta 2 anyway.

I’m using HEAD of the qt-qml staging branch. contentItem was introduced in change 9d6ccfea89ae99b747f70ece71185868f189d0f9 (June 24) — I’m not sure if it is in beta2; if not it will definitely be in the next release candidate.

Regards,
Michael

 
  ‹‹ "Unselect" current item in ListView      QML objects accesible in QtScript? ››

You must log in to post a reply. Not a member yet? Register here!