+ Reply to Thread
Results 1 to 6 of 6

Thread: How to Change QTreeView highlight color

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to Change QTreeView highlight color

    I am curious if there is a way to change the default blue highlight color for a QTreeView item, when a particular situation occurs. For instance if you make a QTreeView from data contained in say a QHash, based on a value in the QHash you want the highlight color to be green instead of blue.

    How would you do this?

    Thanks for any help!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to Change QTreeView highlight color

    I am not aware of a convenience method for that, but I am not that heavy user of model/view.
    Generally speaking you should override the delegate paint() method, and insert the specific logic you want for : option.state & QStyle::State_Selected
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to Change QTreeView highlight color

    Threre is easy way:
    QWidgte::palette-prop and QPalette, QPalette::ColorRole-enum == QPalette::Highlight.
    Last edited by MarekR22; 6th May 2011 at 13:08. Reason: fixing link

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to Change QTreeView highlight color

    And how you would use set palette in this case?
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to Change QTreeView highlight color

    A sorry, I didn't notice that he wants different colors for different nodes of tree (my fault).
    So he have to deliver own delegate like you said.

    anyway it still quite simple. Subclass QStyledItemDelegate and override paint method like that:
    Qt Code:
    1. void QStyledItemDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. if (index.data(someRole).toInt()==1 && option.state.testFlag(QStyle::State_Selected)) {
    4. QStyleOptionViewItem newOption(option);
    5. newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight , someOtherBrush);
    6. QStyledItemDelegate::paint(painter, newOption, index);
    7. return;
    8. }
    9. QStyledItemDelegate::paint(painter, option, index);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by MarekR22; 6th May 2011 at 16:18.

  6. #6
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to Change QTreeView highlight color

    This might have went over my head a little bit but I think I see what you are both saying...

    So if I currently have each tree item as class TreeItem where a private variable contains the data (as mentioned a QHash/QList/QVector...). Then those items are added to the tree model class called TreeModel which is a sub-class of AbstractItemModel.

    Your saying to sub-class QStyledItemDelegate... would that be the TreeItem or TreeModel? My guess is TreeItem because that seems to make more sense to me but I see where you say index.data which leads me to believe your talking about the AbstractItemModel.

    Just in case your curious the TreeModel class is then attached to QTreeView with QTreeView::setModel(TreeModel).

    I hope I am making myself clear but if you need anything else please let me know and thanks for any suggestions!

+ Reply to Thread

Similar Threads

  1. Highlight an item in QCombobox by text color
    By AlekseyK in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2011, 22:06
  2. Replies: 3
    Last Post: 22nd January 2010, 16:46
  3. Replies: 3
    Last Post: 17th November 2009, 23:01
  4. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25
  5. Setting a highlight text color for QTableWidgetItem
    By Hiba in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2007, 10:51

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