Results 1 to 2 of 2

Thread: How to access stylesheet color values programatically

  1. #1
    Join Date
    Dec 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to access stylesheet color values programatically

    Hello,

    I've written a custom delegate for QTreeView / QTreeWidget, but I'm having a bit of trouble.

    My MainWindow has the following in its stylesheet :
    Qt Code:
    1. background-color: #000000;
    2. alternate-background-color: #111111;
    3. }
    4. QTreeView::item:selected:active {
    5. background: #222222;
    6. }
    7. QTreeView::item:selected:!active {
    8. background: #333333;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Without the delegate, this works fine: I get alternating row colors, and different colors for the currently selected row, depending on whether or not the widget is enabled.

    In my delegate, I have a drawBackground() method that does the following:
    Qt Code:
    1. QPalette::ColorGroup cg = QPalette::Disabled;
    2. if (option.state & QStyle::State_Enabled) {
    3. if (option.state & QStyle::State_Active)
    4. cg = QPalette::Normal;
    5. else
    6. cg = QPalette::Inactive;
    7. }
    8.  
    9. if (option.showDecorationSelected && (option.state & QStyle::State_Selected))
    10. painter->fillRect(background, option.palette.brush(cg, QPalette::Highlight));
    11. else
    12. painter->fillRect(background, option.palette.brush(cg, (index.row() % 2 ? QPalette::AlternateBase : QPalette::Base)));
    To copy to clipboard, switch view to plain text mode 

    This code works for the alternating (unselected) rows, however, the color used for the currently selected (highlighted) row is wrong. It doesn't seem to match any of the colors actually defined in my stylesheet.

    Long story short, what is the correct way to access the QSS "QTreeView::item:selected:active->background" value programatically?

  2. #2
    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: How to access stylesheet color values programatically

    There is no way to access this value. If you wish to have control over the colours, set them via QPalette instead of style sheets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 7
    Last Post: 1st November 2011, 07:55
  2. How to access QString characters values
    By rdelgado in forum Newbie
    Replies: 7
    Last Post: 15th November 2010, 14:01
  3. How to access the lineEdits values in TableView
    By grsandeep85 in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2009, 18:15
  4. how to access widgets values?
    By BalaQT in forum Qt Programming
    Replies: 4
    Last Post: 22nd August 2009, 12:06
  5. Replies: 3
    Last Post: 10th April 2009, 14:49

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.