2012-10-05 3 views
0

내 사용자 지정 테이블 모델은 QAbstractTableModel에서 파생되고 QTableView에 표시됩니다.QTableView/사용자 지정 테이블 모델 : 머리글에 텍스트 색 설정

은 다음과 같습니다

enter image description here

내가 모델에 결정 될 수있는 특정 행 헤더의 텍스트 색상을 변경하고 싶습니다. 거기에서 특정 헤더를 색칠 할 수 있습니까? 나는 지금까지 길을 찾을 수 없었다. 내가 발견 한 것은 특별한 헤더가 아닌 모든 헤더에 대해 배경/텍스트 색상을 설정하는 것이 었습니다. 색상은 사용자에게 일종의 마크 업이라고 가정합니다.

답변

2

QAbstractTableModel::headerData()을 다시 구현해야합니다. 섹션의 값 (0에서 시작하는 머리글 인덱스)에 따라 개별적으로 머리글 항목의 스타일을 지정할 수 있습니다. Qt::ItemDataRole 전경 (= 텍스트 색상)과 배경 관련 값 일예 Qt::BackgroundRoleQt::ForegrondRole

있다

이렇게 :

QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const { 
    //make all odd horizontal header items black background with white text 
    //for even ones just keep the default background and make text red 
    if (orientation == Qt::Horizontal) { 
    if (role == Qt::ForegroundRole) { 
     if (section % 2 == 0) 
      return Qt::red; 
     else 
     return Qt::white; 
    } 
    else if (role == Qt::BackgroundRole) { 
     if (section % 2 == 0) 
      return QVariant(); 
     else 
     return Qt::black; 
    } 
    else if (...) { 
     ... 
     // handle other roles e.g. Qt::DisplayRole 
     ... 
    } 
    else { 
     //nothing special -> use default values 
     return QVariant(); 
    } 
    } 
    else if (orientation == Qt::Vertical) { 
     ... 
     // handle the vertical header items 
     ... 
    } 
    return QVariant(); 
}