Search code examples
c++qttextqpainter

What is the vertical offset between 2 lines of text in QFont?


Is it just QFontMetrics::height(), or is it QFontMetrics::height()+QFontMetrics::leading()?

For example, in this code:

painter.drawText(QRect(x, y, 1000, 1000), "line1");
y += lineOffset;
painter.drawText(QRect(x, y, 1000, 1000), "line2");
y += lineOffset;
painter.drawText(QRect(x, y, 1000, 1000), "line3");

What would the value of lineOffset need to be in order for the lines to be spaced as if this code had been used:

painter.drawText(QRect(x, y, 1000, 1000), "line1\nline2\nline3");

Solution

  • As the documentation tells you, lineSpacing() is the vertical distance between two lines, and it is the sum of height() (the height of the font) and leading().