2012-07-12 3 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

이 값은 상수입니까? 모델의 인스턴스에서이 함수를 호출 할 때마다 동일한 브러시가 반환됩니까? 그렇지 않은 경우 헤더 데이터가 변경되었다는 사실을 알리기 위해 관련 신호를 내 보냅니 까? –

답변

12

당신은 더 많은 정보를 위해 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); 
} 
관련 문제