Results 1 to 5 of 5

Thread: QListWidget/QTreeWidget, etc selected item color

  1. #1
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget/QTreeWidget, etc selected item color

    Is there an easy way to change the color of a selected item in QListWidget and QTreeWidget, or do I have to do this by overriding the paint method in an ItemDelegate?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget/QTreeWidget, etc selected item color

    Yes. Use setData() with Qt::TextColorRole.

  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: QListWidget/QTreeWidget, etc selected item color

    I think he means the selection color. QItemDelegate uses QPalette::Highlight for drawing the item selection.
    Qt Code:
    1. QPalette p = treeWidget->palette();
    2. p.setColor(QPalette::Highlight, Qt::red);
    3. // or even different colors for different color groups (states)
    4. // p.setColor(QPalette::Normal, QPalette::Highlight, Qt::green);
    5. // p.setColor(QPalette::Disabled, QPalette::Highlight, Qt::blue);
    6. treeWidget->setPalette(p);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    Arthur (15th May 2006)

  5. #4
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget/QTreeWidget, etc selected item color

    Thanks, it works.

    However it does not work when the item is editable
    Do you know a solution for that as well?

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget/QTreeWidget, etc selected item color

    You probably need to use QPalette::Active colourgroup.

Similar Threads

  1. Replies: 4
    Last Post: 25th September 2008, 13:54
  2. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  3. Changing selected item color in non-current window.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2007, 07:09
  4. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 16:00
  5. Replies: 1
    Last Post: 23rd March 2006, 06:37

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.