Results 1 to 4 of 4

Thread: Do it yourself alternating row colors.

  1. #1
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Do it yourself alternating row colors.

    Hello !

    I am using a subclassed QAbstractItemView and my model is a subclass of a QAbstractItemModel.

    In my model's 'data' function I want to return a QBrush variant for the role, Qt.BackgroundRole. However I want to return an alternating row color depending on how the index is positioned in the view.

    "So why don't you use QAbstractItemView.setAlternatingRowColors ?" You might ask.

    Well .. the problem is two fold: Problem one is that there is only one selection color in a QAbstractItemView so you have D.I.Y. if you want to alternately color selected rows yourself. Problem two is that if you are doing some complex delegate drawing, you might want to know what brush the background is painted with.

    So my question is this ... What is the best way to find out, from within the model, if you are painting an even or odd row ? Right now I'm caching this information by keeping a parallel data structure to the model, finding out what is not visible due to it being collapsed or filtered etc and then calculating odd and even that way. But this is a pain and it breaks when you start using view sorting.

    Can Qt help me .... can I hijack what ever the internal mechanism is that makes setAlternatingRowColors work somehow ?

    Thanks for any ideas.

    barnaby.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Do it yourself alternating row colors.

    The model can tell only based on its indices. However, as you often will wrap the model in a sorting or filtering proxy model, this approach will not really work.

    Try using a style sheet. From the style sheet docs
    Supports the box model. When alternating row colors is enabled, the alternating colors can be styled using the alternate-background-color property.

    The color and background of the selected item is styled using selection-color and selection-background-color respectively.
    HTH

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

    barnabyr (1st November 2008)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Do it yourself alternating row colors.

    Quote Originally Posted by barnabyr View Post
    Well .. the problem is two fold: Problem one is that there is only one selection color in a QAbstractItemView so you have D.I.Y. if you want to alternately color selected rows yourself. Problem two is that if you are doing some complex delegate drawing, you might want to know what brush the background is painted with.
    Leave the model alone, trying to do it there is a wrong path. All you need is to modify the delegate - it knows if it's painting a "base" or "alternate base" colour through the use of QStyleOptionViewItemV2, so you can do all the background painting you want there.

  5. The following user says thank you to wysota for this useful post:

    barnabyr (1st November 2008)

  6. #4
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: Do it yourself alternating row colors.

    Quote Originally Posted by wysota View Post
    Leave the model alone, trying to do it there is a wrong path. All you need is to modify the delegate - it knows if it's painting a "base" or "alternate base" colour through the use of QStyleOptionViewItemV2, so you can do all the background painting you want there.
    Hi wysota,

    Many thanks for the insightful reply. Yes that is what I should do. I only have 4.3 right now but an upgrade to 4.4 would be most useful as I could then use a QStyledItemDelegate and call initStyleOption which would initialize all those useful properties like the 'Alternate' feature.

    barnaby.

Similar Threads

  1. QTreeView -- Alternating Row Colors Oddity
    By chuckshaw in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 19:41
  2. row colors
    By nategoofs in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2007, 19:16
  3. Fonts, Colors, and QStyle
    By Jimmy2775 in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 07:26
  4. Qt Designer 4.0.1 Preview Colors
    By jtyler in forum Qt Tools
    Replies: 2
    Last Post: 14th February 2006, 15:47
  5. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 24th January 2006, 23:12

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.