这可能是 Qt4.7 和 Qt4.8 之间的回归。每个段落都应该在单独的行上,但是在 4.8 上它被破坏了。你知道一些解决方法吗?
#!/usr/bin/env python3
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class CustomLabel(QLabel):
def __init__(self, text):
super(CustomLabel, self).__init__(text)
self._text = text
def paintEvent(self, event):
brect = QRect() #Qt.rect()
painter = QPainter(self)
painter.fillRect(brect, Qt.transparent)
doc = QTextDocument(self)
stylesheet = "*{color: " + painter.pen().color().name() + "; line-height:0.3; margin:0; padding:0;}"
doc.setDefaultStyleSheet(stylesheet)
doc.setUndoRedoEnabled(False)
doc.setHtml(self._text)
doc.setUseDesignMetrics(True)
doc.drawContents(painter, QRectF())
print(doc.toHtml())
print(doc.toPlainText())
app = QApplication(sys.argv)
label = CustomLabel("<p><font style=\"font-size: 9px;\" color=\"#fffe51\">Text 2</font></p><p><font size=\"9px\" color=\"#aaaaaa\">Text 3</font></p>")
label.show()
label.resize(100, 100)
sys.exit(app.exec_())