Qt :: BackgroundRole似乎被忽略 [英] Qt::BackgroundRole seems to be ignored

查看:7465
本文介绍了Qt :: BackgroundRole似乎被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是来自 QAbstractTableModel 的自定义表模型。

I'm using a custom table model derived from QAbstractTableModel.

我覆盖了 headerData(),我可以通过返回 Qt :: ForegroundRole

I've overwritter headerData() and I can change the font color for individual row-headers (or column-header, but I'm all about rows here) by returning the color on Qt::ForegroundRole

if(role == Qt::ForegroundRole)
   return Qt::green;

但如果我去 Qt :: BackgroundRole 设置标题单元格的背景颜色,不会发生任何变化。

But if I go for Qt::BackgroundRole to set the background color of the header cells, nothing happens.

if(role == Qt::BackgroundRole)
   return Qt::red;

我在 return 上设置了断点它达到了。但没有发生任何事情: - (

I set a breakpoint on the return and it is reached. But nothing happens :-(

任何有关我错误的想法?

Any ideas on where I'm wrong?

推荐答案

AFAIK的角色颜色相当于设置调色板颜色, QStyle 绘制标题单元格可以随意忽略它。

AFAIK the role colours are equivalent to setting a palette colour, the QStyle drawing the header cells is free to ignore it.

我使用 QPalette 或样式表在窗口小部件上设置任意颜色时遇到了麻烦,文本倾向于工作,窗口例如, QPushButton 背景),但文本输入字段背景不(例如 QLineEdit ),但AFAIK下面是特定的 QStyle 实现,因此不仅会在不同的窗口小部件,而且在不同的平台上有所不同,只有某种想要重新实现 QStyle (一个作业),或者在 paintEvent(..)(很难遵循当前的 QStyle ,仍然有很多代码)。

I've had trouble using QPalette or style sheets to set arbitrary colours on widgets. Text tends to work, as do 'window' coloured backgrounds (a QPushButton background for example), but text entry field backgrounds don't (QLineEdit for example). But AFAIK it's down to the particular QStyle implementation, so will vary not only across widgets, but also across platforms. The only certain way to get things exactly how you want is to reimplement QStyle (a big job), or paint it manually in paintEvent(..) (difficult to follow the current QStyle and still lots of code).

这篇关于Qt :: BackgroundRole似乎被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆