In QTreeView, how to use the enabled sibling column's background colour in disabled column in delegate paint method
-
Hello all,
I am using Qt 6.5. I am facing issue to set the background colour of a disabled column in a QTreeView for 3 conditions when the row is selected, hovered and hovered on top of the already selected column.
There is a difference of colours for all these 3 different conditions.The other columns of the view are not disabled and I see the right background colours for them.
By default, the background colour of a disabled column is white. Setting the colours in the css file for different options does not work for the disabled column for the QTreeView!
QTreeView
{
&::item:selected { background: #cde6eb; } //light blue1
&::item:disabled { background: #cde6eb; } //light blue1 //Temporary ones here.
&::item:hover { background: #bedfe6; } //light blue2
}The disabled column has the flag ~Qt::ItemIsEnabled set in the model while other columns are enabled. Also, the Model::data() does not have Qt::Background role set as the QTreeView takes the colours from the css file only.
This Treeview's model has a delegate (subclassed from QStyledItemDelegate).
I am trying to use overriden paint method in the delegate to paint the background colour of a disabled column for the 3 conditions.void ModelDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { ... else if (index.column() == Model::DisabledColumn) { if (option.state & QStyle::State_Selected) == QStyle::State_Selected) { //Does not reach option_copy.backgroundBrush=QBrush(QColor(218, 236, 240)); } if (option.state & QStyle::State_MouseOver) { //Reaches here option_copy.backgroundBrush=QBrush(QColor(218, 236, 240)); } } ... QStyle * style = option.widget ? option.widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option_copy, painter); return; }Note that the condition for State_Selected does not get hit but it gets hit for State_MouseOver. Seems this happens when ~Qt::ItemIsEnabled is set for the disabled column. Unfortunately, I need to keep this for the
disabled column. So, I cannot use the below for State_MouseOver & State_Selected.The other columns of the treeview have proper background colour for these 3 conditions taken from the css file.
So, I would like to know how I can get the background colour of other enabled columns for these 3 conditions so that I can use them for the disabled column for the same conditions in the delegate paint method.
Thanks in advance.