QTextEdit with custom space between lines
Hi,
I cannot figure it out how to have a TextEdit area where I can specify the space between the lines, I could subclass a QWidget and calculate every single line and paint it with drawText.
I think there's a better way to do that with TextLayout but I don't know how to use it, any help is appreciated
Thanks in advance
Bye
Re: QTextEdit with custom space between lines
You have to do your own layouting. If you take a look at QTextLayout docs, there is a pseudo-code snippet that demonstrates how the layout is done. It contains references to line height. If you want more light between lines, you have to adjust the "height" variable there and relayout lines.
Re: QTextEdit with custom space between lines
Thanks for the tip,
I created a widget with the TextLayout and it works fine, but I noticed that newline are ignored by QtextLayout, so if I do:
Code:
textLayout.setText("Test \n \n Hello");
int leading = fontMetrics.leading();
int height = 0;
qreal widthUsed = 0;
textLayout.beginLayout();
while (1) {
if (!line.isValid())
break;
line.setLineWidth(width());
height += leading;
line.
setPosition(QPoint(0, height
));
height += line.height()+space;
widthUsed = qMax(widthUsed, line.naturalTextWidth());
}
textLayout.endLayout();
when drawing there's no white lines between Test and Hello but they're both written on the same line,
is there a way to make Qtetxlayout consider \n ?
thanks
Bye
Re: QTextEdit with custom space between lines
I don't think so. QTextLayout is used to lay out a single paragraph of text, so it won't handle multiple paragraphs by itself. I think you need to break text into paragraph and iterate over them with QTextLayout yourself.
Sorry for the long delay.
Re: QTextEdit with custom space between lines
thanks for replying, I'll try to spilt my text into paragraphs, I just thought there was something similar already done in Qt, maybe with the oppurtunity to format the text style using Html just like in QTextEdit.