View Full Version : QPlainTextEdit and geometry

12th August 2014, 13:10

i am trying to port over some legacy wxwidgets code to Qt5 and am running into problems. Basically, there is one textbox that contains text elements. Aligned next to the textbox there is an empty widget used for custom drawing. The custom drawings must now be aligned with the elements in the textbox next to it. elements may span an arbitrary amount of linebreaks, but i cant even get it to work with one line. To make things easier, i set a mono-space font.

The problem is now that i cant get the textlines aligned with my drawing on the widget.

There seem to be two issues:

1) at the beginning of the documents, there seems to be some sort of paragraph-break, ie some pixel lines of extra-empty space.

2) the line height of a text line returned by QFontMetric(font()).height() appears to be off by a little, or there is some linespacing i dont see.

this picture illustrates the two points:


code used for inserting the text line in to the QPlainTextEdit:

for(auto vil : items)

code used for drawing in the empty widget:

for (int i = 0; i < lines; i++)
p->drawLine(width() - 5 - ((i % 2) ? 3 : 0), i*LineHeight(),
width() - 5 - ((i % 2) ? 3 : 0), (i + 1)*LineHeight());

int LineHeight()
return QFontMetrics(font()).height();

help is appreciated. :)

12th August 2014, 14:16
For line spacing see QFontMetrics::lineSpacing()


12th August 2014, 14:50
lineSpacing() returns the same value as height(). :/

Added after 8 minutes:

it looks like the line spacing is hard coded to 1px. if i do height() + 1, it is aligned correctly, provided i scroll down a little.

That leaves me with the problem of the initial spacing at the very top of the textedit.