QPainterPath.addText drawing Chinese too low and interfere with underline
-
I am using QPainterPath to add text and then fill the text in an QGraphicsItem. I also want to give background to the text, so I use QFontMetricsF to get bounding rect for the text and draw a transparent rect as background. I use QPainterPath because I need to draw outline below my test, so large outline width wouldn't block my text.
The thing is: when I draw Chinese with underline, the bottom of the text is touching the underline, which should not happen in other text editor like MS Word. Also, the bounding rect I get from QFontMetricsF does not contain the text inside.
Without underline:

The bounding rect seems positioned a bit upper. Leaving the bottom part of the Chinese out of the rect. Is this a normal behavior?With underline:

English seems to be ok. Chinese letters have their bottom touching the underline. I tested several fonts, but it's always like this, so it's probably not about the fonts.I am running PySide6 on Windows 10.
And below is the demo :from PySide6.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsItem, QGraphicsTextItem from PySide6.QtGui import QFont, QBrush, QPen, QPainterPath, QFontMetricsF, QColor from PySide6.QtCore import Qt, QRect import sys class textItem(QGraphicsItem): def __init__(self): super().__init__() self.text = '''这是一个测试字符串。This is a test string.''' self.font = QFont() self.font.setFamily('Arial') self.font.setPixelSize(100) self.font.setUnderline(True) self.fontMetrics = QFontMetricsF(self.font) self.rect = self.fontMetrics.boundingRect(self.text) self.textPath = QPainterPath() self.textPath.setFillRule(Qt.WindingFill) self.textPath.addText(-self.rect.width() / 2, self.rect.height() / 2, self.font, self.text) self.textBrush = QBrush(Qt.black, Qt.SolidPattern) def boundingRect(self): return QRect(-960, -540, 1920, 1080) def paint(self, painter, option, widget): painter.fillRect(-self.rect.width() / 2, -self.rect.height() / 2 , self.rect.width(), self.rect.height(), QColor.fromRgb(0, 255, 0, 128)) painter.fillPath(self.textPath, self.textBrush) if __name__ == '__main__': app = QApplication() mainWindow = QMainWindow() view = QGraphicsView() scene = QGraphicsScene() view.setScene(scene) scene.addItem(textItem()) mainWindow.setCentralWidget(view) mainWindow.showMaximized() sys.exit(app.exec())So am I missing some setting to place the underline lower or something? And why is the QFontMetricsF gives the bounding rect a bit upper ?