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