簡體   English   中英

如何更改QTableView的標題背景顏色

[英]How to change the header background color of a 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);
    }
    //...
}

如何設置背景顏色?

您可以在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 );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM