QTreeView, how to use the enabled sibling column's background colour in disabled column in delegate paint method

46 Views Asked by At

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 conditions does not work for the disabled column for the QTreeView!

This is a qt bug reported in Qt6 and as mentioned in their comments, they won't fix this issue. QTBUG-112192: QTreeView read only columns can not be selected.

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 and Qt::ItemIsSelectable 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 below 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)
      {
        QStyleOptionViewItem option_copy(option);
        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 use disabled flag for the disabled column as the disabled pseudo state has been used in the css file to use different images for the checkbox based on different conditions, etc. Moreover, there is another checkbox which has enabled status in another column in the same QTreeView and so both needs to be differentiated.

So, I cannot use the State_MouseOver & State_Selected condition in the paint method. The other columns of the treeview have proper background colour for these 3 conditions taken from the css file.

Finally, 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 without adding specific if conditions for selection/hovering.

Thanks in advance.

0

There are 0 best solutions below