2012-07-12 53 views
5

以下是我目前嘗試的內容。標題文本正確更改顏色,但背景不會從默認更改。如何更改QTableView的頁眉背景色

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

如何設置背景顏色?

+0

該值是否爲常數 - 每次在模型實例上調用此函數時都返回相同的畫刷嗎?如果不是,您是否發出相關信號來通知視圖頭部數據已更改? – 2012-07-12 22:40:18

回答

4

這裏有一個替代的解決方案設置樣式表以獲得更多信息。

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

這個解決方案不適用於我使用Qt 5.9.1,但樣式表解決方案! – ForeverLearning 2017-10-11 14:52:31