Custom style for a QTableWidgetItem
-
Hi,
I'm trying to make a custom QTableWidgetItem having different background colors coming from a style sheet for different types of data it stores.
Presume I have 2 types of data, A and B, I make something like:
@
#define A_TYPE 1002
#define B_TYPE 1003class CustomTableItem : public QObject, public QTableWidgetItem
{
Q_OBJECTpublic:
CustomTableItem ( QString text, int type );
};CustomTableItem::CustomTableItem ( QString text, int type ) : QTableWidgetItem(text, type)
{
if( type == A_TYPE )
setProperty("property-A", true);
if( type == B_TYPE)
setProperty("property-B", true);
}
@I have also a .qss file with:
@
*[property-A="true"] { background-color: yellow; }
*[property-B="true"] { background-color: red; }
@
I try now to fill in a QTableWidget:@
QTableWidgetItem* item = new CustomTableItem("type A data", A_TYPE);
table->setItem(0, 0, item);item = new CustomTableItem("type B data", B_TYPE);
table->setItem(1, 0, item);
@I expect to get different bg colors for the two items inserted (the qss file is loaded in the main() with qApp->setStyleSheet(styleSheet); ). The result is a table having the same color for both type A and B items.
Any help is welcome. Thanks.
Eugen -
It must be a lot of coincidence but there is another thread that just started on the same subject: http://developer.qt.nokia.com/forums/viewthread/2240/
It is a little more generic but may provide you with another path until someone can answer you here.
-
I know the links, not solving my problem. Thanks anyway!
-
Hi,
you can't style the cells of a table, that's not possible.
You can style the table itself or the alternate rows, but not rows depending on properties etc.
We also tried that and had a support request, it's not possible.The style sheet works on the widget base, and the properties work on the widget base or on some special pseudo child properties but not on view items.
-
I presumed this but I can't believe. You can style "everything" expect table's cells on props...
-
You can style nearly everything, but style sheets have limitations. Cells of vies are not stylable by content (as the items you create are not objects for the style), QCalendarWidget can't be styles (can it in 4.7.1?), and there are some more restrictions.
If you style a widget depending on properties and change those properties, the style is not reaplied (not in 4.6.3, don't know whether it is on 4.7.1) and you can't force that, as repolishStyle is a private function in QWidget :-(So there are some cases that do not work and where we created workarounds for our needs (for some of them) in our company.
What we do to change the background col,or is we overwrite the BQt::BackgroundRole for the table model and return the needed color, that works :-)) Ok, it's not in the style sheet, but it's a compromise...
-
The more you use style sheets the more limitations you'll run into. Sometimes you have to drop into code to do the customization.
-
Hi,
But as long as you can write something like below:
@
QTableWidgetItem *item = new QTableWidgetItem("item text");
item->setBackground(QBrush(QColor(1,2,3));
table->setItem(0,0,item);
@
and get the wanted color for the table's cell, I expected a related "stylable" mechanism to be available. In the end the item is treated like a widget (even not inheriting widget class) and should support more or less the style props that every box has. If not, I expected that subclassing from QObject will give me the freedom I need. Maybe this will be available in the future versions. Big miss imo.I can make my own style sheet format and load it at runtime and get the wanted colors but I would have preferred a uniform way of dealing with the UI elements.
thanks for answers,
eugen -
But you only have objects, if you use QTabelWidget, what do you do with QTabelView (which is the defacto base)? I think, QTableView should be used more that QTableWidget, especially, as it allows custom models. So you would need to specify dataRoles in the style sheet to get the needed info to style the item.
That would get really strange I think.