How to change the header background color of a QTableView

andre picture andre · Jul 12, 2012 · Viewed 19.9k times · Source

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?

Answer

Jimmy picture Jimmy · Jul 12, 2012

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