Results 1 to 7 of 7

Thread: Setting a highlight text color for QTableWidgetItem

  1. #1
    Join Date
    Dec 2007
    Posts
    6

    Question Setting a highlight text color for QTableWidgetItem

    Hi,

    A small, but annoying problem with colors in Qt 4.2.3.

    I have a QWidgetTable that contains 3 colums of QWidgetTableItems and several rows. I want to have the last column's text to be in different color, and changed depending of the value. When the row is not selected this works ok with the SetForeground() -function, and giving it a QBrush. However, since the user can select entire row from the table, one row will always be highlighted, and the last column item's colorsettings do not apply anymore for this highlighted row.

    How I can set the HighlightedText color for the QWidgetTableItem, without changing the highlight background color (I want to have the whole row highlighted, and the last column text to not change color)? This would be easy if the QWidgetTableItem could be given a normal QPalette, but it seems you can give only QBrush and only for background/foreground roles.

    If I have to make an own subclass of the QWidgetTableItem, I could use some tips how to do this since I'm quite new to the Qt.

  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: Setting a highlight text color for QTableWidgetItem

    Why don't you just use setBackground() instead of setForeground() ?
    ==========================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
    Dec 2007
    Posts
    6

    Default Re: Setting a highlight text color for QTableWidgetItem

    Quote Originally Posted by high_flyer View Post
    Why don't you just use setBackground() instead of setForeground() ?
    Hmm, am I missing something very simple here? I don't want to change the background color when the item is not highlighted, I want be able to define the color of the actual text inside the QTableWidgetItem when it's highlighted.

    Example of a wanted situation:
    Item is not selected/highlihted: Background color is black, text color is red.
    -This works fine.

    Item is selected/highlighted: Background color is grey, text color is red.
    -This does not work, since the text color get's the highlighted text color set for the widget, not keeping the red value when it's being highlighted. Background color is ok.
    - I can't change the highlighted text colors of the whole QTableWidget, I have to change the colors only for this specific column. Or can you specify a palette for a entire column in QTableWidget?
    Last edited by Hiba; 14th December 2007 at 09:17.

  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: Setting a highlight text color for QTableWidgetItem

    In that case have a look at QPalette I must admit, I find the various names confusing, and I always do a trail and error test before I get the colors right the way I want them.
    I suggest you look at the QPalette docs for your item, and try the various enums that might make sense:
    QPalette::Text
    QPalette::Window
    QPalette::WindowText
    QPalette::Foreground
    ==========================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
    Dec 2007
    Posts
    6

    Default Re: Setting a highlight text color for QTableWidgetItem

    I can set the QPalette correct for the entire QTableWidget, but the problem comes when setting the QPalette for specific part of the widget, in this case for a one column. The table contains QTableWidgetItem:s, and I can change the colors for these individual cells using setForeground() and setBackground() for text and background respectively. But I don't know how to set the other color roles for this QTableWidgetItem or set the QPalette for it, and as the result it uses it's parents QPalette settings when the cell is highlighted. Do I have to make my own my_TableWidgetItem that reimplements the painting? Seems to be a bit excessive for a feature of this size.

  6. #6
    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: Setting a highlight text color for QTableWidgetItem

    Sorry, I assumed QTableWidgetItem was a QWidget, I see it is not...
    I didn't do much with tables in Qt, so I am not 100% sure, but it looks to me you will have to subclass QTableWidgetItem if you want this on item level or the QTableWidget if you want this on table level.
    But maybe some other users here who are more experienced with QTableWidget and friends can help more.
    ==========================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.

  7. #7
    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: Setting a highlight text color for QTableWidgetItem

    Isn't this somewhat similar problem than in this thread?
    J-P Nurmi

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Replies: 2
    Last Post: 23rd July 2012, 09:42
  3. QTreeWidgetItem setting color for the text
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2007, 18:22
  4. Setting color in a QTreeWidget text
    By brevleq in forum Qt Programming
    Replies: 4
    Last Post: 10th August 2007, 20:21
  5. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 08:25

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.