I have created my own delegate to be able to have a custom style for my QListWidgetItem's. Now I want to use alternatingRowColors for the QListWidgetItem's but I'm not sure how to change the paint method in my delegate to get alternatigRowColors working. How can I change the background color if the row is alternating?

This is how my code for paint method looks like (a little bit simplified):
Qt Code:
  1. void ListDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
  2. QRect r = option.rect;
  3.  
  4. if(option.state & QStyle::State_Selected){
  5. painter->setBrush(QBrush(Qt::red, Qt::SolidPattern));
  6. painter->drawRect(r);
  7. } else {
  8. painter->setBrush(QBrush(Qt::white, Qt::SolidPattern));
  9. painter->drawRect(r);
  10. }
  11.  
  12. //GET TITLE
  13. QString title = index.data(Qt::DisplayRole).toString();
  14.  
  15. //TITLE
  16. r = option.rect.adjusted(45, 0, -10, -30);
  17. painter->setFont( QFont( "Arial", 6, QFont::Normal ) );
  18. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, title, &r);
  19. }
To copy to clipboard, switch view to plain text mode