The following is what I've currently tried. The header text changes color correctly but the background will not change from the default.
template<typename T>
inline QVariant TableModel<T>::headerData(int section, Qt::Orientation orientation, int role) const
{
//...
else if(role == Qt::BackgroundRole) {
return QBrush(m_display.headerBackground);
}
//...
}
How can I set the background color?
To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.
Go to the Design tab. Click Customize to expand the set of choices for customizing your theme. Click Header Image to choose an image to be the background of the header. Click Header background to choose a color for the header section.
By default the header of a Tabular report is white. In order to customize the Table header color, you can do it by adding either CSS or Javascript Code. Also, you can use color name or its hex code and can customize the table header color accordingly.
You can set the style sheet on the QTableView
ui->tableView->setStyleSheet("QHeaderView::section { background-color:red }");
for more info see http://doc.qt.io/qt-4.8/stylesheet-examples.html
Here's an alternative solution.
MyTableView::MyTableView( QWidget* parent ) : QTableView( parent )
{
...
// Make a copy of the current header palette.
QPalette palette = horizontalHeader()->palette();
// Set the normal/active, background color
// QPalette::Background is obsolete, use QPalette::Window
palette.setColor( QPalette::Normal, QPalette::Window, Qt::red );
// Set the palette on the header.
horizontalHeader()->setPalette( palette );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With