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

18

以下是我目前尝试过的。标题文本可以正确改变颜色,但背景无法从默认值更改。

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

我该如何设置背景颜色?


这个值是常量吗 - 每次在模型实例上调用此函数时,返回的是相同的画笔吗?如果不是,您是否发出相关信号来通知视图标题数据已更改? - Kuba hasn't forgotten Monica
2个回答

36

这仍然使得垂直和水平表头之间的“拐角”没有样式,那叫什么?它与表格内容不同。 - Jim
1
@Jim QTableView中的角落小部件是作为QAbstractButton实现的,可以使用“QTableView QTableCornerButton :: section”选择器进行样式设置。 (来自https://doc.qt.io/qt-5/stylesheet-reference.html#qtableview-widget) - Matt Binford

2
这里有一个替代方案。
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 );
}

2
这个解决方案在我使用Qt 5.9.1时不起作用,但是样式表解决方案可以! - ForeverLearning

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接