在不利用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); }");
效果如下:
转载于:https://www.cnblogs.com/r0xFED/p/10110864.html
相关资源:QT QListVIew mvd例程