参考博客: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);
}
}
本文介绍了一个Qt中自定义Model的示例代码,展示了如何使用QAbstractItemModel实现不同角色的数据展示,如文本显示、背景颜色、文字颜色等。
1492

被折叠的 条评论
为什么被折叠?



