自定义 QStyledItemDelegate:添加粗体项目

发布于 2024-08-04 10:35:12 字数 1957 浏览 2 评论 0原文

故事是这样的:

我有一个 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)

我现在面临的问题:

  1. 正常(非粗体)项目是 稍微缩进(几个像素)。 这可能是一些默认设置 行为。我可以将我的项目缩进 也很大胆,但是接下来会发生什么 在不同的平台下?
  2. 通常,当我选择项目时,会有一个小边框,周围有虚线(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:

  1. 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?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-08-11 10:35:12

我没有测试过这个,但我认为你可以这样做:

class TypeSoortDelegate(QStyledItemDelegate):

def paint(self, painter, option, index):
    get value...
    if value:
        option.font.setWeight(QFont.Bold)

    QStyledItemDelegate.paint(self, painter, option, index)

I've not tested this, but I think you can do:

class TypeSoortDelegate(QStyledItemDelegate):

def paint(self, painter, option, index):
    get value...
    if value:
        option.font.setWeight(QFont.Bold)

    QStyledItemDelegate.paint(self, painter, option, index)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文