Results 1 to 2 of 2

Thread: Selection color for progressbar in treeview

  1. #1
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Selection color for progressbar in treeview

    I have a treeview where some items contain a progressbar. This I fixed by subclassing QStyledItemDelegate and re-implementing the paint method. This works well and looks good.

    I do have a problem and that is that when I select a row in the tree, the cell containing the progress-bar doesn't change its background color to the selection color. The background color stays the same as the non-selected rows which makes it look very weird.

    This is my paint-method in the delegate:

    Qt Code:
    1. void ResourceTreeDelegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem& option,
    3. const QModelIndex& index) const
    4. {
    5. int column = index.column();
    6. if (column == ResourceTreeModel::Progress) {
    7.  
    8. painter->save();
    9.  
    10. if (index.data().isNull()) {
    11. QStyledItemDelegate::paint(painter, option, index);
    12. }
    13. else {
    14. QColor backgroundColor = qvariant_cast<QColor>(index.data(Qt::BackgroundColorRole));
    15. QColor highlightColor = option.palette.color(QPalette::Highlight);
    16.  
    17. int value = index.data().toInt();
    18. if (value < 0) value = 0;
    19. int x = option.rect.x();
    20. int y = option.rect.y();
    21. int width = option.rect.width();
    22. int height = option.rect.height();
    23.  
    24. // make with margins
    25. int margin = ResourceTreeDelegateConstants::PROGRESS_BAR_MARGIN;
    26. int progressBarHeight = ResourceTreeDelegateConstants::PROGRESS_BAR_HEIGHT;
    27. QRect newRect(x + margin,
    28. y + (int)(height / 2.0) - 7,
    29. width - (2 * margin),
    30. progressBarHeight);
    31.  
    32. QStyleOptionProgressBar progressBarOption;
    33. progressBarOption.palette.setColor(QPalette::Window, backgroundColor);
    34. progressBarOption.palette.setColor(QPalette::Highlight, highlightColor);
    35. progressBarOption.rect = newRect;
    36. progressBarOption.minimum = 0;
    37. progressBarOption.maximum = 100;
    38. progressBarOption.progress = value;
    39. progressBarOption.text = QString(" %1%").arg(QString::number(value)).leftJustified(7, ' ');
    40. progressBarOption.textVisible = true;
    41.  
    42. painter->fillRect(option.rect, progressBarOption.palette.background());
    43. QApplication::style()->drawControl(QStyle::CE_ProgressBar,
    44. &progressBarOption, painter);
    45. }
    46.  
    47. if (index.isValid()) {
    48. painter->setPen(Qt::lightGray);
    49. painter->drawRect(option.rect);
    50. }
    51.  
    52. painter->restore();
    53. }
    54.  
    55. QStyledItemDelegate::paint(painter, option, index);
    56. }
    To copy to clipboard, switch view to plain text mode 

    How can I tell the progressbar to change its background when rows are selected in the tree?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Selection color for progressbar in treeview

    You need to check for the selected state. Something like this should work:
    Qt Code:
    1. if(option.state & QStyle::State_Selected){
    2. // set selected colors - QPalette::Highlight & QPalette::HighlightedText
    3. }
    4. else {
    5. // set unselected colors if necessary - QPalette::Window & QPalette::Text
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    Jennie Bystrom (14th February 2011)

Similar Threads

  1. troubles with changing progressBar color
    By dilidpan in forum Qt Programming
    Replies: 13
    Last Post: 15th September 2012, 02:58
  2. Replies: 1
    Last Post: 27th October 2010, 18:06
  3. TreeView and selection updating
    By Tepi in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2010, 09:07
  4. Replies: 7
    Last Post: 1st March 2010, 17:49
  5. Selection color
    By jnana in forum Qt Programming
    Replies: 1
    Last Post: 10th November 2006, 13:28

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.