Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter::drawStaticText() line spacing is too long (mac)
Forum Updated to NodeBB v4.3 + New Features

QPainter::drawStaticText() line spacing is too long (mac)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 865 Views 2 Watching
  • 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.
  • J Offline
    J Offline
    jun1160
    wrote on last edited by jun1160
    #1

    When drawing Japanese text using QPainter :: drawStaticText (), the line spacing becomes too long.
    It does not occur with English text.
    And, it does not occur on Windows.

    The environment is as follows.

    OS: mac os x12.1
    Qt Creator: 5.0.2
    Qt:6.2.2 for mac

    The following is the drawing result.

    sampleimage.png

    Below is the result of running the same project on Windows.

    sample2.png

    Does anyone know why such a problem arises?
    I want to use QPainter::drawStaticText() to draw like QPainter::drawText().

    The code is below.

    void MyWidget::paintEvent(QPaintEvent */*event*/) {
    
        QPainter painter(this);
    
        QString japaneseString("あああ いいい ううう えええ おおお");
        QString englishString("aaa bbb ccc ddd eee fff ggg");
    
        QTextOption option;
        option.setWrapMode(QTextOption::WrapMode::WrapAtWordBoundaryOrAnywhere);
    
        // draw QStaticText (japanese)
        {
            QStaticText stext;
            stext.setText(japaneseString);
            stext.setTextWidth(100);
            stext.setTextOption(option);
            stext.prepare();
    
            auto size = stext.size(); // <-- height is too long (bug?)
    
            painter.drawStaticText(QPoint(10, 30), stext); // <-- draw height is too long (bug?)
    
            painter.save();
            painter.setPen(Qt::red);
            painter.drawRect(QRect(10, 30, size.width(), size.height()));
            painter.restore();
        }
    
        // draw Text (japanese)
        {
            auto frame = QRect(200, 30, 100, 200);
            painter.drawText(frame, japaneseString, option);
        }
    
        // draw QStaticText (english)
        {
            QStaticText stext;
            stext.setText(englishString);
            stext.setTextWidth(100);
            stext.setTextOption(option);
            stext.prepare();
    
            auto size = stext.size();
    
            painter.drawStaticText(QPoint(10, 130), stext);
    
            painter.save();
            painter.setPen(Qt::red);
            painter.drawRect(QRect(10, 130, size.width(), size.height()));
            painter.restore();
        }
    
        // draw Text (english)
        {
            auto frame = QRect(200, 130, 100, 200);
            painter.drawText(frame, englishString, option);
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should also add the fonts that get used on each platform.

      On a related note, you should define your QStaticText objects in your constructor otherwise you shoot yourself in the foot since you recreate them each time paintEvent is called which is what you want to avoid when using QStaticText.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        You should also add the fonts that get used on each platform.

        On a related note, you should define your QStaticText objects in your constructor otherwise you shoot yourself in the foot since you recreate them each time paintEvent is called which is what you want to avoid when using QStaticText.

        J Offline
        J Offline
        jun1160
        wrote on last edited by jun1160
        #3

        @SGaist

        Thank you for your reply.

        The font is not specified programmatically.
        According to the API reference, the default constructor for the QFont class is used.

        // QStaticText reference
        QStaticText::prepare(const QTransform &matrix = QTransform(), const QFont &font = QFont());
        
        // And, QFont reference
        QFont::QFont()
        Constructs a font object that uses the application's default font.
        

        When I tried to output the QFont in QStaticText and QPainter with the following code,
        it was as follows.

        ...
        
        QTransform matrix = QTransform();
        QFont font = QFont();
        stext.prepare(matrix, font);
        qDebug() << font;
        
        painter.setFont(font);
        qDebug() << painter.font();
        
        ...
        

        Patterns I tried

        • QStaticText::drawStaticText() (Japanese)
        • QStaticText::drawStaticText() (English)
        • QPainter::drawText() (Japanese)
        • QPainter::drawText() (English)

        The output was:

        [Mac]
        QFont(.AppleSystemUIFont,13,-1,5,400,0,0,0,0,0,0,0,0,0,0,1)
        (All 4 patterns)

        [Windows]
        QFont(Yu Gothic UI,9,-1,5,400,0,0,0,0,0,0,0,0,0,0,1)
        (All 4 patterns)

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jun1160
          wrote on last edited by
          #4

          When I tried it with Qt ver.6.2.3, the problem was fixed.
          Probably the Qt's bug has been fixed.
          Close this matter.

          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 Extensions
          • Unsolved