带有html富文本委托的PyQt listview将文本位移动到不适当的位置(包括图片和代码)
获得了一个列表视图,其中的项目需要使用粗体文本,因此html委托是一个不错的选择。我环顾四周,找到了几种解决方案,并从中生成了这段代码,但它存在一个问题,您可以在gif中看到,它显示了使用委托和不使用委托的结果带有html富文本委托的PyQt listview将文本位移动到不适当的位置(包括图片和代码),qt,listview,delegates,pyqt,pyside,Qt,Listview,Delegates,Pyqt,Pyside,获得了一个列表视图,其中的项目需要使用粗体文本,因此html委托是一个不错的选择。我环顾四周,找到了几种解决方案,并从中生成了这段代码,但它存在一个问题,您可以在gif中看到,它显示了使用委托和不使用委托的结果 from PyQt4.QtCore import * from PyQt4.QtGui import * import sys class HTMLDelegate(QStyledItemDelegate): def __init__(self, parent=None):
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_())
这里有两个版本
我不使用sizeHint,因为通过我的测试,我觉得它并没有影响这种行为。
某些属性错误地定位了文本,我已经能够用以下方法部分地反驳它:
self.doc.setDocumentMargin(0)
默认边距为4,然后将整个项目位向右移动
rect = options.rect
options.rect = QRect(rect.x()+3, rect.y(), rect.width(), rect.height())
但是我不认为它解决了这个问题的原因,它只是掩盖了它,当你尝试添加时,它再次成为一个问题。经过更多的挖掘和尝试,调整文本矩形似乎给人一种感觉,即一切都是正确的,并且在不同的DEs中表现一致
textRect.adjust(-1, -4, 0, 0)
我不确定这是否是一个bug,但人们会认为
SE_ItemViewItemText
应该自己给出正确的位置这是我现在使用的一个
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_())
这是一个老问题,但由于我遇到了一个类似的问题,我想我应该用您的代码解决一些问题。主要是self.doc成员。它不会像你希望的那样起作用。在调用paint()之前(多次)调用了sizeHint()方法,并且将得到一个空文档(这就是为什么您“感觉它没有影响此行为”) 您可能会想,只需在sizeHint()和paint()中初始化文档然后,但这也会是错误的。您的列表中只有一个委托人和文档,因此文档始终基于最后一个大小或绘制的委托人和文档,因此将self.doc作为成员只会为您节省文档构造,考虑到它会导致混乱,这可能不值得