QPainter::drawStaticText() line spacing is too long (mac)
-
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 macThe following is the drawing result.
Below is the result of running the same project on Windows.
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); } }
-
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.
-
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)