QTreeView 中某些索引的自定义文本颜色
我想使用自定义颜色(取决于与每行相关的数据)在 QTreeView 小部件的一列中绘制文本。我尝试重载drawRow()受保护的方法并更改样式选项参数,如下所示(一个精简的示例):
virtual void drawRow(QPainter* p_painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyleOptionViewItem optionCustom = option;
if (index.column() == 2)
{
optionCustom.palette.setColor(QPalette::Text, Qt::red);
}
QTreeView::drawRow(p_painter, optionCustom, index);
}
但显然我错过了一些东西,因为这似乎不起作用(我还尝试更改QPalette ::WindowText
颜色作用)。
I would like to draw texts in one of the columns in a QTreeView widget using a custom color (depending on the data related to each row). I tried to overload the drawRow() protected method and change the style option parameter like this (a stripped-down example):
virtual void drawRow(QPainter* p_painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyleOptionViewItem optionCustom = option;
if (index.column() == 2)
{
optionCustom.palette.setColor(QPalette::Text, Qt::red);
}
QTreeView::drawRow(p_painter, optionCustom, index);
}
But obviously I am missing something because this does not seem to work (I tried to change also the QPalette::WindowText
color role).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的模型中,扩展
data()
函数以返回给定颜色作为Qt::ForegroundRole
角色。例如:
In your model, extend the
data()
function to return a given color as theQt::ForegroundRole
role.For example: