今天犯了一个非常低级的错误
属于没有认真看文档的问题,
想要给Qtableview根据右键菜单的设置,给每一行设置不同的颜色,
模型是的QSqlrelationtablemodel,直接用setData一直都不成功。
最后选择继承这个类,
重写Data函数,
问题是先定义QBrush brush;
brush.setColor(Qt::blue);
然后就
switch(role)
{
case Qt::ForegroundRole:
return QVariant(QBrush(QColor(Qt::yellow)));
case Qt::BackgroundRole://called to paint the background
return QVariant(QBrush(QColor(Qt::gray))); //ok
return QVariant(brush);//not ok,because QBrush style is set default to Qt::no brush
default://some other role we don't really care about
return QSqlRelationalTableModel::data(index,role);
} 证明,只要给它设置style就可以了。 折腾了一天,原来是这个问题。