PDA

View Full Version : QPlainTextEdit and geometry



tuli
12th August 2014, 12:10
hi,

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:

https://i.imgur.com/Q1uqK5b.png


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


for(auto vil : items)
{
cur.insertBlock();
cur.insertText("testesttest");
cur.block().setUserData(...);
}


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. :)

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

Cheers,
_

tuli
12th August 2014, 13: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.