Qt MVD框架下修改檢視中資料項/標題的背景顏色說明(Qt 5.10.1)
阿新 • • 發佈:2018-12-12
在不利用QSS的情況下,如果想修改MVD中檢視的某個資料項的背景顏色,則可以:
QVariant YourModel::data(const QModelIndex &index, int role) const { // ... if(role == Qt::BackgroundRole) return QColor(222, 31, 45); // ... }
但是,按照這種思路,卻無法修改MVD中檢視的header中的某個資料項的背景顏色。
QVariant YourModel::headerData(int section, Qt::Orientation orientation, int role) const { // ... if(role == Qt::BackgroundRole) return QColor(123, 22, 44); // ... }
所以,如果需要修改header的背景色,似乎必須用QSS了:
// 紫色背景色 tableView->horizontalHeader()->setStyleSheet("QHeaderView::section { background-color: rgb(123, 33, 201); }");
效果如下: