如何更改QTableView的标题背景色

2024-09-10 21:41:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

如何设置背景色


Tags: 文本标题内容颜色inlinesectiontemplateqt
2条回答

可以在QTableView上设置样式表

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

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

这里有一个替代方案

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

相关问题 更多 >