Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. In QTreeView, how to use the enabled sibling column's background colour in disabled column in delegate paint method
Qt 6.12 LTS is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 308 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadMan2016
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved