QLabel setpalette not working in cellWidget
-
Hi!
I want to set the background color of QLabel in the QTableWidget cell using the QLabel setPalette method, nothing happens, what could be the reason?
label = new QLabel(this); label->setAutoFillBackground(true); p = label->palette(); p.setColor(QPalette::Window, Qt::red); p.setColor(QPalette::WindowText, Qt::red); label->setPalette(p); setCellWidget(0, 1, label);
Thanks in advance.
-
Hi,
Please write your question in English as it is the main language of the forum.
Or use the Hungarian sub-forum if you want to stay in your native language.
-
Why are you using a QLabel as cell widget ?
-
@Kutyus
Hi
but TableWidget already supports other colors for the background ?So cant you just use that ?
Or did i miss something ? :)
You do it via the items.
QBrush brush1(QColor(0, 255, 127, 255));
brush1.setStyle(Qt::SolidPattern);
QTableWidgetItem *item = new QTableWidgetItem(); ( or take existing )
item->setBackground(brush1); -
@Kutyus said in QLabel setpalette not working in cellWidget:
Would I put the device icon in the cell and the background would be green or red, depending on whether it is connected?
Don't use a cell widget but simply use what QTableWidgetItem has - it's enough for your use case (text, an icon and setting the (background) color)
-
@mrjj said in QLabel setpalette not working in cellWidget:
But do you need that cell widget ?
I mean the item can have icon and color but maybe you also show
other stuff not easy to make with item alone ?I tried this solution because I couldn't find how to center the icon.
-
@Kutyus
Hi
Ok, well centering the icon is not supported out of the box
and you would need to use a Delegate for that with custom painting.
https://forum.qt.io/topic/83265/centering-decorationrole-icon-in-qtableview-cell/9Anyway, if the palette is not working you can use a stylesheet to set labels color.