PDA

View Full Version : How to make table headers with custom background?



kremuwa
14th March 2010, 15:21
As in the thread title. I'm using QTableWidget Class to represent data in table.

I would like these headers:
http://img251.imageshack.us/img251/4339/przechwytywaniebk.jpg

To look (for example) like this:
http://img688.imageshack.us/img688/2886/przechwytywanieb.jpg

Is that possible? How to do it?

toutarrive
14th March 2010, 16:51
You can achieve it by using style sheet file.

Create a file and name it mystyle.qss
To get custom header :


QHeaderView::section {
border: 0.5px solid #C2C7CB;
padding-left: 4px;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #59AAFC,
stop: 0.2 #C2C7CB,
stop: 0.5 #DFDFDF,
stop: 0.7 #C2C7CB,
stop: 1.0 #287ACC );
}


Set up the style for your app in the main function:


int main ( int argc, char* argv[])
{
QApplication app(argc,argv);
QFile file(":/mystyle.qss");
if (file.open(QFile::ReadOnly));
QString styleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(styleSheet);

// other code

return app.exec();
}


Create a resource file (myresource.qrc) and include your style file:


<RCC>
<qresource >
<file>mystyle.qss</file>
</qresource>
</RCC>


And finally add myresource.qrc in the application *.pro file:


RESOURCES = myresource.qrc

(In this example, the files created are relative to your app directory).
If you need to customize other widgets just add them to your qss file.

I encourage you to read about Qt resource and style sheet concepts.
Have a look at http://doc.trolltech.com/4.6/stylesheet-customizing.html to get more information.