qt - 带有 html 富文本委托(delegate)的 PyQt listview 将文本位移出位置(包括图片和代码)

标签 qt listview delegates pyqt pyside

gif showing with delegate and without

有一个 ListView ,其中的项目需要使用粗体文本,因此 html 委托(delegate)是可行的方法。我环顾四周,找到了几个解决方案,并从中编写了这段代码,但它存在您可以在 gif 中看到的问题,该图显示了使用委托(delegate)和不使用委托(delegate)的结果。

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class HTMLDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super(HTMLDelegate, self).__init__(parent)
        self.doc = QTextDocument(self)

    def paint(self, painter, option, index):
        painter.save()

        options = QStyleOptionViewItemV4(option)
        self.initStyleOption(options, index)

        self.doc.setHtml(options.text)
        options.text = ""

        style = QApplication.style() if options.widget is None \
            else options.widget.style()
        style.drawControl(QStyle.CE_ItemViewItem, options, painter)

        ctx = QAbstractTextDocumentLayout.PaintContext()

        if option.state & QStyle.State_Selected:
            ctx.palette.setColor(QPalette.Text, option.palette.color(
                                 QPalette.Active, QPalette.HighlightedText))

        textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
        painter.translate(textRect.topLeft())
        self.doc.documentLayout().draw(painter, ctx)

        painter.restore()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    data = ['1','2','3','4','5','6','7','8','9']
    main_list = QListView()
    main_list.setItemDelegate(HTMLDelegate())
    main_list.setModel(QStringListModel(data))
    main_list.show()
    sys.exit(app.exec_())

这里是PyQt5PySide版本

我不使用 sizeHint,因为通过我的测试,感觉它并没有影响此行为。 某些属性错误地定位了文本,我已经能够部分地反击它:

self.doc.setDocumentMargin(0)

默认边距为4,然后将整个item位向右移动

rect = options.rect
options.rect = QRect(rect.x()+3, rect.y(), rect.width(), rect.height())

但我不认为它解决了这个问题的原因,它只是掩盖了它,一旦你尝试添加icons for example,它就会再次成为问题。

最佳答案

我现在用的就是这个

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys

class HTMLDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super().__init__()
        self.doc = QTextDocument(self)

    def paint(self, painter, option, index):
        painter.save()

        options = QStyleOptionViewItem(option)

        self.initStyleOption(options, index)
        self.doc.setHtml(options.text)
        options.text = ""

        style = QApplication.style() if options.widget is None \
            else options.widget.style()
        style.drawControl(QStyle.CE_ItemViewItem, options, painter)

        ctx = QAbstractTextDocumentLayout.PaintContext()

        print(QStyle.State_Selected)

        if option.state & QStyle.State_Selected:
            ctx.palette.setColor(QPalette.Text, option.palette.color(
                QPalette.Active, QPalette.HighlightedText))
        else:
            ctx.palette.setColor(QPalette.Text, option.palette.color(
                QPalette.Active, QPalette.Text))

        textRect = style.subElementRect(
            QStyle.SE_ItemViewItemText, options)

        if index.column() != 0:
            textRect.adjust(5, 0, 0, 0)

        thefuckyourshitup_constant = 4
        margin = (option.rect.height() - options.fontMetrics.height()) // 2
        margin = margin - thefuckyourshitup_constant
        textRect.setTop(textRect.top() + margin)

        painter.translate(textRect.topLeft())
        painter.setClipRect(textRect.translated(-textRect.topLeft()))
        self.doc.documentLayout().draw(painter, ctx)

        painter.restore()

    def sizeHint(self, option, index):
        return QSize(self.doc.idealWidth(), self.doc.size().height())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    data = ['1','2','3','4','5','6','7','8','9']
    main_list = QListView()
    main_list.setItemDelegate(HTMLDelegate())
    main_list.setModel(QStringListModel(data))
    main_list.show()
    sys.exit(app.exec_())

关于qt - 带有 html 富文本委托(delegate)的 PyQt listview 将文本位移出位置(包括图片和代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175644/

相关文章:

c++ - Qt - 从底部删除菜单?

html - 带有 <hr> 行的 QML 文本

JavaFX - ListView 不填充 fxml 中的数据

asp.net - ASP.net 的虚拟 ListView ?

ios - 如何通过页脚单元格在表格 View 中使用委托(delegate)?

c# - 委托(delegate) P/Invoke 传递的实例方法

qt - Qt下的HTTP GET

c++ - Release模式下的 Qt 调试 - 所有方法均不起作用

android - 使用 ListView 和自定义适配器设置数据

ios - 使多个对象成为另一个对象的委托(delegate)的最佳方法是什么?