PDA

View Full Version : QTextEdit with custom space between lines



Lele
27th October 2006, 12:21
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

wysota
27th October 2006, 13:02
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.

Lele
30th October 2006, 11:42
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:



QFontMetrics fontMetrics(QApplication::font());

textLayout.setText("Test \n \n Hello");

int leading = fontMetrics.leading();
int height = 0;
qreal widthUsed = 0;
textLayout.beginLayout();
while (1) {
QTextLine line = textLayout.createLine();
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

wysota
4th November 2006, 09:50
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.

Lele
22nd November 2006, 09:24
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.