Results 1 to 10 of 10

Thread: QTreeWidgetItem and background color

  1. #1
    Join Date
    Oct 2007
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeWidgetItem and background color

    How can i change the background color of a qtreewidgetitem?
    When item has a parent, and e.g 3 columns then
    Qt Code:
    1. QBrush b( Qt::red );
    2. item->setBackground( 0, b );
    3. item->setBackground( 1, b );
    4. item->setBackground( 2, b );
    To copy to clipboard, switch view to plain text mode 
    doesn't work because first column( 0 ) isn't a really first column, and i have this:
    Attached Images Attached Images

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem and background color

    Try setting QWidget::setAutoFillBackground on the item.
    Hope this helps

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidgetItem and background color

    Would you like the whole row to be filled with background color?
    Attached Images Attached Images
    J-P Nurmi

  4. #4
    Join Date
    Oct 2007
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem and background color

    @aamer4yu: doesn't work
    @jpn: yes. How can i do that?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidgetItem and background color

    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
    J-P Nurmi

  6. #6
    Join Date
    Oct 2007
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem and background color

    I need help:
    Qt Code:
    1. void MyTree::drawRow( QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    2. {
    3. painter->save();
    4. QBrush brush( QColor( 255, 240, 180 ) );
    5. painter->fillRect( rect, brush ) // how can i get rect ( QRect ) ?
    6. painter->restore();
    7. QTreeWidget::drawRow( painter, option, index );
    8. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem and background color

    Quote Originally Posted by swiety View Post
    how can i get rect ( QRect ) ?
    Try the one in the option variable.

  8. #8
    Join Date
    Feb 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTreeWidgetItem and background color

    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...

  9. #9
    Join Date
    Apr 2010
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidgetItem and background color

    // how can i get rect ( QRect ) ?

    do get rect pls use option.rect

  10. #10
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTreeWidgetItem and background color

    The next line will do the trick:

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.