Discussion:
[Development] QTextEdit - Line Spacing
coroberti .
2018-11-11 13:11:52 UTC
Permalink
Hi,
It seems that arranging line spacing for text in QTextEdit (like 1.5,
2, 3 lines) is not an easy coding.

Any code samples, directions, howtos would be very much appreciated.

Thanks in advance!

Kind regards,
Robert
Allan Sandfeld Jensen
2018-11-11 15:06:27 UTC
Permalink
Post by coroberti .
Hi,
It seems that arranging line spacing for text in QTextEdit (like 1.5,
2, 3 lines) is not an easy coding.
Any code samples, directions, howtos would be very much appreciated.
There is probably a way to get to the default or root QTextBlockFormat and set
lineHeight on it, but I couldn't spot the logical way from QTextEdit either;
but you can always just do it in CSS and set the CSS as the default style-
sheet on the document the QTextEdit is showing. Remember a QTextEdit is
basically showing HTML, so you can do it like you would in HTML.

'Allan
coroberti .
2018-11-11 15:13:12 UTC
Permalink
Post by Allan Sandfeld Jensen
Post by coroberti .
Hi,
It seems that arranging line spacing for text in QTextEdit (like 1.5,
2, 3 lines) is not an easy coding.
Any code samples, directions, howtos would be very much appreciated.
There is probably a way to get to the default or root QTextBlockFormat and set
lineHeight on it, but I couldn't spot the logical way from QTextEdit either;
but you can always just do it in CSS and set the CSS as the default style-
sheet on the document the QTextEdit is showing. Remember a QTextEdit is
basically showing HTML, so you can do it like you would in HTML.
'Allan
Hi Allan,
Thanks, but It seems that only this HTML/CSS subset is supported:

http://doc.qt.io/qt-5/richtext-html-subset.html

Best regards,
Robert
coroberti .
2018-11-11 16:15:56 UTC
Permalink
Kind regards,
Robert Iakobashvili
............................
Post by coroberti .
Post by Allan Sandfeld Jensen
Post by coroberti .
Hi,
It seems that arranging line spacing for text in QTextEdit (like 1.5,
2, 3 lines) is not an easy coding.
Any code samples, directions, howtos would be very much appreciated.
There is probably a way to get to the default or root QTextBlockFormat and
set lineHeight on it, but I couldn't spot the logical way from QTextEdit
either; but you can always just do it in CSS and set the CSS as the
default style- sheet on the document the QTextEdit is showing. Remember a
QTextEdit is basically showing HTML, so you can do it like you would in
HTML.
'Allan
Hi Allan,
http://doc.qt.io/qt-5/richtext-html-subset.html
Right, line-height isn't mentioned there, but considering the C++ side of the
CSS implementation (QTextBlockFormat) has it, it might just have left out. I
would give it a shot.
'Allan
http://doc.qt.io/qt-5/qtextblockformat.html#setLineHeight
http://doc.qt.io/qt-5/qtextblockformat.html#LineHeightTypes-enum ->
QTextBlockFormat::ProportionalHeight1This sets the spacing
proportional to the line (in percentage). For example, set to 200 for
double spacing.
Thank you.
Kind regards,
Robert
Following Allan's advise, here's something that is starting to work
(checks omitted)

QTextDocument* doc = this->text_edit_->document();
QTextBlock currentBlock = doc->firstBlock();

while (currentBlock.isValid()) {

QTextCursor cursor(currentBlock);
QTextBlockFormat blockFormat = currentBlock.blockFormat();
blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight);
cursor.setBlockFormat(blockFormat);

currentBlock = currentBlock.next();
}

Thank you very much!
Robert
coroberti .
2018-11-13 06:50:12 UTC
Permalink
Post by coroberti .
Following Allan's advise, here's something that is starting to work
(checks omitted)
QTextDocument* doc = this->text_edit_->document();
QTextBlock currentBlock = doc->firstBlock();
while (currentBlock.isValid()) {
QTextCursor cursor(currentBlock);
QTextBlockFormat blockFormat = currentBlock.blockFormat();
blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight);
cursor.setBlockFormat(blockFormat);
currentBlock = currentBlock.next();
}
Thank you very much!
Isn't it simpler to use a selection approach instead?
QTextBlockFormat format;
format.setLineHeight(...);
QTextCursor cursor(textDocument);
cursor.select(QTextCursor::Document);
cursor.mergeBlockFormat(format);
My 2 c,
--
Thanks, Guiseppe. It also works properly.

Thanks, Allan.
So, there are two good ways from the API point of view.

When HTML from QTextEdit is retrieved by toHtml(), it generates HTML
with the correct line-height, i.e. 200% as in the example below:

<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px;
margin-right:0px; -qt-block-indent:0; text-indent:0px;
line-height:200%;">

It's properly treated by browsers, etc.

However, when saved to an html file and next loaded to QTextEdit,
line-height attribute inside <p> tag is ignored.
This page doesn't refer to line-height as to the supported attributes:
http://doc.qt.io/qt-5/richtext-html-subset.html#block-attributes

Is it correct to say that this is not a bug but rather an expected behavior,
and bug report is not required for this case?
Thanks.

Kind regards,
Robert Iakobashvili
............................

Loading...