PDA

View Full Version : QTreeWidgetItem and background color



swiety
4th February 2008, 21:11
How can i change the background color of a qtreewidgetitem?
When item has a parent, and e.g 3 columns then


QBrush b( Qt::red );
item->setBackground( 0, b );
item->setBackground( 1, b );
item->setBackground( 2, b );

doesn't work because first column( 0 ) isn't a really first column, and i have this:

aamer4yu
5th February 2008, 07:38
Try setting QWidget::setAutoFillBackground on the item.
Hope this helps

jpn
5th February 2008, 07:51
Would you like the whole row to be filled with background color?

swiety
5th February 2008, 10:22
@aamer4yu: doesn't work
@jpn: yes. How can i do that?

jpn
5th February 2008, 10:31
Here's an idea:

subclass QTreeWidget
reimplement QTreeView::drawRow()
fill the whole row with desired background color
call the base class implementation to draw actual content

swiety
5th February 2008, 22:17
I need help:


void MyTree::drawRow( QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
painter->save();
QBrush brush( QColor( 255, 240, 180 ) );
painter->fillRect( rect, brush ) // how can i get rect ( QRect ) ?
painter->restore();
QTreeWidget::drawRow( painter, option, index );
}

jacek
5th February 2008, 22:25
how can i get rect ( QRect ) ?
Try the one in the option variable.

alena
19th March 2014, 19:50
I got the rectangle from the option with option->rect but the items are still with the default background color...
I am trying to figure this one for the past couple of days... Swiety, if you have figured it out can you please share with me your way of solving it :)
I also tryed (sorry about the syntax error I translated this from Python to C++ since my code is in Python):
painter->fillRect(option->rect, QColor(233, 233, 233))
option->palette->setBrush(QPalette->Base, QColor(255, 0, 0))
This will give me the whole tree background to be grey (QColor(233,233,233) insted of being red... And I cannot find the connection between the painter and the palette...
Please help...

aliks-os
23rd July 2014, 09:13
// how can i get rect ( QRect ) ?

do get rect pls use option.rect

FSDrake
26th October 2016, 07:25
The next line will do the trick:

for (int col = 0; col < treeWidget->columnCount(); col++) TreeWidgetItem->setBackgroundColor(col, QColor(255, 255, 0, 100));