自定义 QStyledItemDelegate:添加粗体项目
故事是这样的:
我有一个 QListview,它使用 QSqlQueryModel 来填充它。由于某些项目应根据模型隐藏列的值以粗体显示,因此我决定创建自己的自定义委托。我正在使用 PyQT 4.5.4,因此从 QStyledItemDelegate 继承是根据文档的方法。我得到了它的工作,但有一些问题。
这是我的解决方案:
class TypeSoortDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
model = index.model()
record = model.record(index.row())
value= record.value(2).toPyObject()
if value:
painter.save()
# change the back- and foreground colors
# if the item is selected
if option.state & QStyle.State_Selected:
painter.setPen(QPen(Qt.NoPen))
painter.setBrush(QApplication.palette().highlight())
painter.drawRect(option.rect)
painter.restore()
painter.save()
font = painter.font
pen = painter.pen()
pen.setColor(QApplication.palette().color(QPalette.HighlightedText))
painter.setPen(pen)
else:
painter.setPen(QPen(Qt.black))
# set text bold
font = painter.font()
font.setWeight(QFont.Bold)
painter.setFont(font)
text = record.value(1).toPyObject()
painter.drawText(option.rect, Qt.AlignLeft, text)
painter.restore()
else:
QStyledItemDelegate.paint(self, painter, option, index)
我现在面临的问题:
- 正常(非粗体)项目是 稍微缩进(几个像素)。 这可能是一些默认设置 行为。我可以将我的项目缩进 也很大胆,但是接下来会发生什么 在不同的平台下?
- 通常,当我选择项目时,会有一个小边框,周围有虚线(Windows 默认的东西?)。在这里我也可以画它,但我想尽可能保持本土化。
现在的问题是:
是否有另一种方法来创建自定义委托,仅在满足某些条件时更改字体粗细,而其余所有条件不变?
我也尝试过:
if value:
font = painter.font()
font.setWeight(QFont.Bold)
painter.setFont(font)
QStyledItemDelegate.paint(self, painter, option, index)
但这似乎完全不影响美观。没有错误,只是默认行为,没有粗体项目。
欢迎所有建议!
So here's the story:
I have a QListview that uses a QSqlQueryModel to fill it up. Because some items should display in bold based on the value of a hidden column of the model, I decided to make my own custom delegate. I'm using PyQT 4.5.4 and thus inheriting from QStyledItemDelegate is the way to go according to the docs. I got it working but there are some problems with it.
Here's my solution:
class TypeSoortDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
model = index.model()
record = model.record(index.row())
value= record.value(2).toPyObject()
if value:
painter.save()
# change the back- and foreground colors
# if the item is selected
if option.state & QStyle.State_Selected:
painter.setPen(QPen(Qt.NoPen))
painter.setBrush(QApplication.palette().highlight())
painter.drawRect(option.rect)
painter.restore()
painter.save()
font = painter.font
pen = painter.pen()
pen.setColor(QApplication.palette().color(QPalette.HighlightedText))
painter.setPen(pen)
else:
painter.setPen(QPen(Qt.black))
# set text bold
font = painter.font()
font.setWeight(QFont.Bold)
painter.setFont(font)
text = record.value(1).toPyObject()
painter.drawText(option.rect, Qt.AlignLeft, text)
painter.restore()
else:
QStyledItemDelegate.paint(self, painter, option, index)
The problems I'm facing now:
- the normal (not bold) items are
slightly indented (a few pixels).
This is probably some default
behaviour. I could indent my item in
bold also, but what happens then
under a different platform? - Normally when I select items there is a small border with a dotted line around it (default Windows thing?). Here also I could draw it, but I want to stay as native as possible.
Now the question:
Is there another way to create a custom delegate that only changes the font weight when some condition is met and leaves all the rest untouched?
I also tried:
if value:
font = painter.font()
font.setWeight(QFont.Bold)
painter.setFont(font)
QStyledItemDelegate.paint(self, painter, option, index)
But that doesn't seem to affect the looks at all. No error, just default behaviour, and no bold items.
All suggestions welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有测试过这个,但我认为你可以这样做:
I've not tested this, but I think you can do: