How to change the header background color of a QTableView

谁都会走 提交于 2019-12-23 08:18:46

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/11454694/how-to-change-the-header-background-color-of-a-qtableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!