QTreeView row background color change not working

I have a QTreeView and different lines require different background colors, depending on their contents. For this, I got the class MyTreeView from QTreeView and implemented the drawing method as follows:

  void MyTreeView::drawRow (QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyleOptionViewItem newOption(option); if (someCondition) { newOption.palette.setColor( QPalette::Base, QColor(255, 0, 0) ); newOption.palette.setColor( QPalette::AlternateBase, QColor(200, 0, 0) ); } else { newOption.palette.setColor( QPalette::Base, QColor(0, 0, 255) ); newOption.palette.setColor( QPalette::AlternateBase, QColor(0, 0, 200) ); } QTreeView::drawRow(painter, newOption, index); } 

First I set setAlternatingRowColors(true); for QTreeView.

My problem: Color setting for QPalette :: Base has no effect . Every second line remains white.

However, setting QPalette :: AlternateBase works as expected . I tried setAutoFillBackground(true) and setAutoFillBackground(false) without any effects.

Are there any tips on how to solve this? Thanks.


Note. Setting the color by adapting MyModel::data(const QModelIndex&, int role) for Qt::BackgroundRole does not give the desired result. In this case, the background color is used only for part of the line. But I want to colorize the entire line, including the left side, using the navigation tree.

Qt Version: 4.7.3


Update: For unknown reasons, QPalette::Base seems opaque. setBrush does not change this. I found the following workaround:

  if (someCondition) { painter->fillRect(option.rect, Qt::red); newOption.palette.setBrush( QPalette::AlternateBase, Qt::green); } else { painter->fillRect(option.rect, Qt::orange); newOption.palette.setBrush( QPalette::AlternateBase, Qt:blue); } 
+7
source share
3 answers

If the only problem is that the expand / drop controls do not have a background similar to the rest of the line, then use Qt::BackgroundRole in ::data() your model (as pnezis described in their answer ) and add this to your tree class structure:

 void MyTreeView::drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const { if (some condition depending on index) painter->fillRect(rect, Qt::red); else painter->fillRect(rect, Qt::green); QTreeView::drawBranches(painter, rect, index); } 

I tested this on Windows (Vista and 7) using Qt 4.8.0, and the expand / collapse arrows have a corresponding background. The problem is that these arrows are part of the view and therefore cannot be processed in the model.

+7
source

Instead of subclassing QTreeView you should handle the background color through your model. Use the data() function and Qt::BackgroundRole to change the background color of the rows.

 QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::BackgroundRole) { if (condition1) return QColor(Qt::red); else return QColor(Qt::green); } // Handle other roles return QVariant(); } 
+5
source

https://www.linux.org.ru/forum/development/4702439

 if ( const QStyleOptionViewItemV4* opt = qstyleoption_cast<const QStyleOptionViewItemV4*>(&option) ) { if (opt.features & QStyleOptionViewItemV4::Alternate) painter->fillRect(option.rect,option.palette.alternateBase()); else painter->fillRect(option.rect,painter->background()); } 
0
source

All Articles