Results 1 to 3 of 3

Thread: delegate and alternatigRowColors, how to?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default delegate and alternatigRowColors, how to?

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: delegate and alternatigRowColors, how to?

    You can use something like that:
    Qt Code:
    1. painter->setBrush( (index.row() % 2) ? Qt::white : Qt::green );
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    martinn (2nd March 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: delegate and alternatigRowColors, how to?

    Thanks really much for your help!

Similar Threads

  1. Another delegate problem...
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 9th August 2009, 14:14
  2. QTreeView Delegate
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2009, 09:10
  3. Delegate question...
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2009, 05:48
  4. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  5. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 20:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.