Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QPainterPath.addText drawing Chinese too low and interfere with underline
Qt 6.12 LTS is out! See what's new in the release blog

QPainterPath.addText drawing Chinese too low and interfere with underline

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 629 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IvanIsLearning
    wrote on last edited by
    #1

    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:
    c1eb60a4-c3a2-4d8e-a3de-72e3c9b55e7d-image.png
    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:
    e4544625-4186-4e2a-a104-5c5a5a226d4b-image.png
    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 ?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved