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 :
background-color: #000000;
alternate-background-color: #111111;
}
background: #222222;
}
background: #333333;
}
QTreeView {
background-color: #000000;
alternate-background-color: #111111;
}
QTreeView::item:selected:active {
background: #222222;
}
QTreeView::item:selected:!active {
background: #333333;
}
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:
if (option.
state & QStyle::State_Enabled) { if (option.
state & QStyle::State_Active) else
}
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)));
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)));
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?
Bookmarks