如何更改QTableView的标题背景颜色

问题描述:

以下是我目前尝试的内容.标题文本可以正确更改颜色,但背景不会更改为默认值.

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);
    }
    //...
}

如何设置背景颜色?

您可以在QTableView上设置样式表

You can set the style sheet on the QTableView

ui->tableView->setStyleSheet("QHeaderView::section { background-color:red }");

有关更多信息,请参见 http://doc.qt.io/qt- 4.8/stylesheet-examples.html

for more info see http://doc.qt.io/qt-4.8/stylesheet-examples.html