PDA

View Full Version : QTableWidget change header background color - QHeaderView



Erik_Klein
23rd September 2009, 12:16
In Short,
Anyone has any clue on how to set background color specific per header in a table???


Guys, would really appreciate some help with this - looked everywhere and nothing working yet.

What im trying to do is to have a tablewidget with 2 distinctive horizontal header styles, so some columns headers will have a colour background whilst another columns will have a different color header background.

The thing is, the basic properties settings(right-click menu) in QT Designer for setting color background looks really buggy and simply does not work, so im trying to do this through stylesheet.

What i've done is....

QHeaderView::section
{
spacing: 10px;
background-color:lightblue;
color: white;
border: 1px solid red;
margin: 1px;
text-align: right;
font-family: arial;
font-size:12px;
}

but this applies to all the headers.
then, if i want multiple background colors, there is only one way to do it which is...

using QHeaderView::section:last or QHeaderView::section:first ,
which would be perfect IF i only had 3 headers to style, but i am working with many different tables containing 6+ columns!

Anyone has any clue on how to set background color specific per header in a table?? Appreciate any help!!

thanks,
Erik

zack
7th July 2010, 14:08
Has anybody found a solution for that?

saa7_go
17th July 2010, 21:07
QHeaderView::section
{
spacing: 10px;
background-color:lightblue;
color: white;
border: 1px solid red;
margin: 1px;
text-align: right;
font-family: arial;
font-size:12px;
}

but this applies to all the headers.


If you want that to applly to a specific header, you can do it manually by code.



QString styleSheet = "::section {" // "QHeaderView::section {"
"spacing: 10px;"
"background-color: lightblue;"
"color: white;"
"border: 1px solid red;"
"margin: 1px;"
"text-align: right;"
"font-family: arial;"
"font-size: 12px; }";

tableWidget->horizontalHeader()->setStyleSheet(styleSheet); // tableWidget->verticalHeader()->setStyleSheet(styleSheet);




then, if i want multiple background colors, there is only one way to do it which is...

Maybe, you can use QTableWidgetItem (http://doc.trolltech.com/4.6/qtablewidgetitem.html) and QTableWidget::setHorizontalHeaderItem(int, QTableWidgetItem*) (http://doc.trolltech.com/4.6/qtablewidget.html#setHorizontalHeaderItem).



QTableWidgetItem *col1 = new QTableWidgetItem("Column 1");
col1->setForeground(Qt::white);
col1->setBackground(QColor("lightblue"));
col1->setFont(QFont("arial", 12));
tableWidget->setHorizontalHeaderItem(0, col1); // tableWidget->setVerticalHeaderItem(0, col1);

But, the background color doesn't work for me.