QTableView设置特定行的颜色
QTableView set the color of a specific row
我知道在论坛和这里有很多关于这个主题的讨论,但我找不到一个有效的解决方案。
我有一个使用模型的QTableView。我需要能够通过模型改变一些特定行的背景颜色,更精确地从data
函数。
QVariant CCustomModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
switch (index.column())
{
case colName: return QVariant(QString::number(1));
case colAdress: return QVariant(QString::number(2));
case colGender: return QVariant(QString::number(3));
case colTelephone: return QVariant(QString::number(4));
default:
return QVariant();
}
}
if(role == Qt::BackgroundColorRole) //also tried Qt::BackgroundRole and Qt::ForegroundRole
{
return QVariant(QColor(Qt::red));
}
return QVariant();
}
这是简单的不工作。数字显示,但背景颜色仍然是基本的。这里有什么可能的解决方案吗?
试试这个:
if(role == Qt::BackgroundRole)
{
return QBrush(QColor(Qt::red));
}
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 设置QTabWidget中各个选项卡的颜色
- 如何仅设置 QTabWidget 背景颜色样式表
- 为什么不能使用 setData() 在 QTreeView 中设置单元格的背景颜色?
- 开罗渐变图案不使用设置颜色
- 在加载页面时,如何为iwebbrowser2设置背景颜色
- Delphi / C 构建器 - 在TDBGrid中设置活动 /选择的行颜色
- 如何设置按钮的背景颜色,包括复选框按钮
- 设置子窗口的文本颜色
- wx颜色设置与整数数组
- 颜色设置在碎片着色器中,未显示GLSL 1.30
- 是否可以设置卷轴的颜色
- 设置 HWND TXT 颜色
- 我如何将特定字符设置为多维数组中的不同颜色
- 如何在wxtreelistctrl中设置文本背景颜色
- 如何在视觉工作室 2012年设置函数输入参数的颜色
- 如何在GDAL输出文件中设置分类颜色
- 为qlineedit设置了背景图像时,如何在qlineedit中设置文本颜色
- 直接在.png加载的cimage上绘制时,将文本颜色设置为
- 单击按钮后对QML矩形的颜色设置动画