PDA

View Full Version : Setting a highlight text color for QTableWidgetItem



Hiba
13th December 2007, 16:16
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.

high_flyer
13th December 2007, 16:33
Why don't you just use setBackground() instead of setForeground() ?

Hiba
14th December 2007, 08:10
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?

high_flyer
14th December 2007, 09:56
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

Hiba
14th December 2007, 10:20
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.

high_flyer
14th December 2007, 10:40
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.

jpn
14th December 2007, 10:51
Isn't this somewhat similar problem than in this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-hover-and-highlight-qtable-and-qtree-items-6507.html)?