<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QPainterPath.addText drawing Chinese too low and interfere with underline]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">Without underline:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/6963a053-45e2-48f0-8edf-1f52b5ca5387.png" alt="c1eb60a4-c3a2-4d8e-a3de-72e3c9b55e7d-image.png" class=" img-fluid img-markdown" /><br />
The bounding rect seems positioned a bit upper. Leaving the bottom part of the Chinese out of the rect. Is this a normal behavior?</p>
<p dir="auto">With underline:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/54c506a2-a14c-4ba2-b513-572007a0296f.png" alt="e4544625-4186-4e2a-a104-5c5a5a226d4b-image.png" class=" img-fluid img-markdown" /><br />
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.</p>
<p dir="auto">I am running PySide6 on Windows 10.<br />
And below is the demo :</p>
<pre><code>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())
</code></pre>
<p dir="auto">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 ?</p>
]]></description><link>https://forum.qt.io/topic/139207/qpainterpath-addtext-drawing-chinese-too-low-and-interfere-with-underline</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 07:12:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139207.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 Sep 2022 07:27:11 GMT</pubDate><ttl>60</ttl></channel></rss>