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,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    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
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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 14:08. Reason: fixing link

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to Change QTreeView highlight color

    And how you would use set palette in this case?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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 17: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!

Similar Threads

  1. Highlight an item in QCombobox by text color
    By AlekseyK in forum Qt Programming
    Replies: 6
    Last Post: 18th March 2019, 22:31
  2. Replies: 3
    Last Post: 22nd January 2010, 17:46
  3. Replies: 3
    Last Post: 18th November 2009, 00:01
  4. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 23:25
  5. Setting a highlight text color for QTableWidgetItem
    By Hiba in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2007, 11: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.