[英]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 }");
這是另一種解決方案。
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.