In my QTreeView, I have a custom delegate set up for one of the columns to display a colored progress bar. The appearance of the progress bar depends on the content and I used the color information from option.palette
to mimic the default delegate behavior.
class ProgressBarDelegate : public QStyledItemDelegate
{
public:
ProgressBarDelegate(QObject *parent = 0) {}
~ProgressBarDelegate() {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// QStyledItemDelegate::paint(painter, option, index); // default implementation
painter->save();
// progressbar construction and drawing
painter->restore();
}
};
Now, I want to highlight a row when hovering over it with the mouse. I added custom CSS to the tree view:
ui->treeView->setStyleSheet("QTreeView::item:hover{background-color:#D0E0F0;}");
The hover highlighting works fine, but unfortunately, my custom delegate does not take into account the CSS settings:
https://i.stack.imgur.com/1BzkN.png
(The dark blue line represents the selected item, while the light blue line is the CSS hover-highlighted row)
If I enable the default implementation, the CSS setting affects my delegate, but it messes up the visual presentation:
https://i.stack.imgur.com/TNR9f.png
The main question is:
- Is there a way to access the overridden CSS settings data within the delegate? It seems like
option.palette
doesn't provide this information.
I tried to read the QTreeView property as suggested here:
qDebug() << treeView()->property("background");
but the console output shows QVariant(Invalid)
I also attempted to look at the Qt source code for the drawControl()
and drawPrimitive()
methods used in the default delegate implementation, but I couldn't find any references to the CSS overrides, only widget palette connections.