[Development] QTextEdit - Line Spacing
coroberti .
coroberti at gmail.com
Sun Nov 11 17:15:56 CET 2018
Kind regards,
Robert Iakobashvili
............................
On Sun, Nov 11, 2018 at 5:29 PM coroberti . <coroberti at gmail.com> wrote:
>
> On Sun, Nov 11, 2018 at 5:21 PM Allan Sandfeld Jensen <kde at carewolf.com> wrote:
> >
> > On Sonntag, 11. November 2018 16:13:12 CET coroberti . wrote:
> > > On Sun, Nov 11, 2018 at 5:06 PM Allan Sandfeld Jensen <kde at carewolf.com>
> > wrote:
> > > > On Sonntag, 11. November 2018 14:11:52 CET coroberti . wrote:
> > > > > 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
> > >
> > 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
>
> Right. You mean to work with:
> 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
More information about the Development
mailing list