参考博客:https://wiki.jikexueyuan.com/project/learn-road-qt/custom-model-second.html
https://blog.csdn.net/weixin_38516302/article/details/107243292
QVariant ABCModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= rowCount())
{
return QVariant();
}
QVariant value = QVariant::Invalid;
int row = index.row();
int col = index.column();
if(role==Qt::DisplayRole)//显示文本数据
{
value = QVariant(QString("Total"));
return value;
}
else if(role==Qt::BackgroundColorRole && index.column()==0)//第一列背景色
{
return QColor(255,0,0);//红色
}
else if(role==Qt::ToolTipRole&&index.column()==0)//第一列提示
{
QString str = "test";
return str;
}
else if(role==Qt::TextAlignmentRole&&index.column()==0)/
{
return Qt::AlignCenter;//将第一列数据居中
}
else if(role==Qt::TextColorRole&&index.column()==1)//第二列字体设置为红色
{
return QColor(255,0,0);
}
}