Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget
-
The setBackground() function CANNOT set the item's background brush to the specified brush on the HorizontalHeaderItem of QTableWidget.
setForeground() function is OK.
Why?
Do you have any advices ? Thanks.
#include <QApplication> #include <QTableWidget> #include <QTableWidgetItem> #include <QHeaderView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableWidget *table = new QTableWidget; table->setColumnCount(4); table->setRowCount(2); QStringList header; header<<"1"<<"2"<<"3"<<"4"; table->setHorizontalHeaderLabels(header); for(int i=0; i<4; i++) { QString strtmp; QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i); columnHeaderItem->setBackground(QBrush(QColor(0,255,0)));///////no effect,why??? columnHeaderItem->setForeground(QBrush(QColor(0,0,255)));///////OK! columnHeaderItem->setText(strtmp.setNum(i)); table->setHorizontalHeaderItem(i,columnHeaderItem); QTableWidgetItem *columnHeaderItem1 = new QTableWidgetItem; columnHeaderItem1->setBackground(QBrush(QColor(0,255,0)));/////OK! columnHeaderItem1->setForeground(QBrush(QColor(255,0,0))); columnHeaderItem1->setText(strtmp.setNum(i+4)); table->setItem(0,i,columnHeaderItem1); QTableWidgetItem *columnHeaderItem2 = new QTableWidgetItem; columnHeaderItem2->setBackground(QBrush(QColor(255,255,0)));///////////////OK! columnHeaderItem2->setForeground(QBrush(QColor(0,0,255))); columnHeaderItem2->setText(strtmp.setNum(i+8)); table->setItem(1,i,columnHeaderItem2); } table->show(); return a.exec(); }
-
try replacing
QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i); columnHeaderItem->setBackground(QBrush(QColor(0,255,0)));
with
QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i); Q_ASSERT(columnHeaderItem); columnHeaderItem->setData(Qt::BackgroundRole,QBrush(QColor(0,255,0)));
-
@VRonin said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:
Q_ASSERT(columnHeaderItem);
Good idea, but It is non null.
The columnHeaderItem->setBackground(QBrush(QColor(0, 255, 0)));
does seems to have no effect :)
When used in header. -
@mrjj said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:
But is it not strange the items can change background color when used in the data cells but not in the cells in HeaderView ?
It is strange indeed, I checked the sources and the background is definitely used in
QHeaderView::paintSection
QVariant backgroundBrush = d->model->headerData(logicalIndex, d->orientation, Qt::BackgroundRole); if (backgroundBrush.canConvert<QBrush>()) { opt.palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush)); opt.palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush)); painter->setBrushOrigin(opt.rect.topLeft()); }
-
@SGaist said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:
unless I'm mistaken
You never are
from QWindowsStyle::drawControl
case CE_HeaderSection: { QBrush fill; if (opt->state & State_On) fill = QBrush(opt->palette.light().color(), Qt::Dense4Pattern); else fill = opt->palette.brush(QPalette::Button);
so, at least in windows, it's ignored