PDA

View Full Version : Setting Background color for QTableWidget horizontal header



jburnette
10th September 2009, 21:37
I am trying to set the background color for the horizontalHeader of my QTableWidget and nothing seems to work. I have tried setting the background color using setStyle( ) to no effect.

QHeaderView* header = new QHeaderView( Qt::Horizontal, this );
QString backgroundStyle = QString( "background-color: rgba(99,0,0,0);\n" );
header->setStyleSheet( backgroundStyle );
QTableWidget* contentTable_ = new QTableWidget(this)
contentTable_->setHorizontalHeader( header );

I have tried changing the brush for the background color role, no effect.

QHeaderView* header = new QHeaderView( Qt::Horizontal, this );
QPalette pp = header->palette();
pp.setBrush( QPalette::Window, QBrush(QColor(Qt::red)));
pp.setBrush( QPalette::Background, QBrush(QColor(Qt::red)));
header->setPalette( pp );
QTableWidget* contentTable_ = new QTableWidget(this)
contentTable_->setHorizontalHeader( header );

I even looked into setting up a delegate for the header so I could override the paint method...nothing (and the documentation even says that QHeaderView ignores delegates).

Sooo.....is it even possible for me to paint my own header or am I stuck with the light gray default that it currently insists on drawing? I am trying to get 'red' to work for now but ultimately I would like to be able to paint a gradient to the background.

I'd appreciate any help. Thanks!

jburnette
11th September 2009, 18:03
Figured it out. For those of you who need to be able to do this you can subclass the QHeaderView and reimplement the paintSection member function. I had tried reimplementing 'paint' which doesn't work...but paintSection does. :)