PDA

View Full Version : QTableWidget Background Color



mbrusati
22nd September 2008, 21:14
This code produces a table of toolbuttons (see attachment). How do I set the background color of the table to something other than its default white background?

#include <QtGui>
#include <QApplication>

int main( int argc, char *argv[] )
{
QApplication app( argc, argv );

QDialog *d = new QDialog;
d->setWindowTitle( "Table Test" );

QTableWidget *table = new QTableWidget( 2, 3 );
QStringList headers = QStringList() << "A" << "B" << "C";
table->setHorizontalHeaderLabels( headers );

table->setGridStyle( Qt::NoPen );
table->setCellWidget( 0, 0, new QToolButton );
table->setCellWidget( 0, 1, new QToolButton );
table->setCellWidget( 0, 2, new QToolButton );
table->setCellWidget( 1, 0, new QToolButton );
table->setCellWidget( 1, 1, new QToolButton );
table->setCellWidget( 1, 2, new QToolButton );

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget( table );

d->setLayout( layout );
d->show();

return app.exec();
}

yuriry
23rd September 2008, 00:13
Style sheets might help you. Try calling setStyleSheet("background: rgb(200, 255, 200)") on your table widget.

Thoosle
23rd September 2008, 18:38
one way is to subclass QTableWidget and repaint the background in the derived classes paint event......