PDA

View Full Version : How to access stylesheet color values programatically



joelafrite
15th April 2013, 17:01
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 :
QTreeView {
background-color: #000000;
alternate-background-color: #111111;
}
QTreeView::item:selected:active {
background: #222222;
}
QTreeView::item:selected:!active {
background: #333333;
}

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:
QPalette::ColorGroup cg = QPalette::Disabled;
if (option.state & QStyle::State_Enabled) {
if (option.state & QStyle::State_Active)
cg = QPalette::Normal;
else
cg = QPalette::Inactive;
}

if (option.showDecorationSelected && (option.state & QStyle::State_Selected))
painter->fillRect(background, option.palette.brush(cg, QPalette::Highlight));
else
painter->fillRect(background, option.palette.brush(cg, (index.row() % 2 ? QPalette::AlternateBase : QPalette::Base)));

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?

wysota
16th April 2013, 20:21
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.