PDA

View Full Version : Lineheight of QPainter vs. QSyntaxHighlighter



tuli
9th June 2018, 02:40
Here is the situation: I have a text string spanning several lines like so


QString str = "111111\n 222222\n 333333\n";


Depending on whether I draw it with a plain QPainter or via a QSyntaxHighlighter, I get different lineheights.
As a result, when drawn next to each other, the lines dont match up.




qpainter.setFont(fixed_font);
qpainter.drawText(rect(), Qt::AlignLeft|Qt::AlignTop, str);

vs.

qsyntaxhighlighter.setFont(fixed_font);
qsyntaxhighlighter.setText(str);
qsyntaxhighlighter.drawFormattedText(qpainter, rect());


Namely the lineheight of the text drawn by the plain qpainter is smaller than that drawn by QSyntaxHighlighter, just by one pixel or so.

I suspect that QPainter is somehow using QFontMetric::lineSpacing() while QSyntaxHighlighter is using QFontMetric::height(). They are 7, resp. 8 on my system and would nicely explain the difference I am seeing.

How can I work around this? Is this a bug?



Environment: Qt 5.11, Ubuntu 17.10, fixed_font = QFontDatabase::systemFont(QFontDatabase::FixedFont )