View Full Version : Vertical alignment with QTextDocument

22nd June 2014, 16:57
I'm using QTextDocument, and i'm looking for a possibility to render a text passage (i.e. QTextFragment, inside a line) with a detailed vertical shift in pt:


E.g. in HTML you would write:

Hello<span style='vertical-align:-5pt;'>Hello deep</span>
<br>Hello Hello Hello Hello</p>

Unfortunately, this seems not to be supported in QTextDocument, neither using it's html interface, nor building the document directly using QTextCursor (and QTextCharFormat).

I know there is a method setVerticalAlignment in QTextCharFormat, but this can only handle with some special modes (top, bottom, ...). What I'm missing is a function to specify the alignment in pt (> 0 -> higher; < 0 -> deeper). The size of the font should not change, only the vertical position.

This alignment is very important for my application (I use in the QTextDocument a mix of normal text fonts and a musical symbol font, and it is a very important feature for the user to align the musical symbols against the text).

I'm using QTextDocument::drawContents for displaying and QTextEdit for editing the document.

Any ideas how to solve this ?

I can't use QtWebKit because it isn't supported on iOS und Android ...

And I know about the nice possibility of inserting a custom object (QTextObjectInterface) into QTextDocument (see Qt textobject example), but this would be not a solution for me because 1. you can customize only the size (-> intrinsicSize()), but not the alignment, and 2. i need a solution for an arbitrary text passage which should stay editable in QTextEdit.

Seems what I have to do is to make a subclass from QAbstractTextDocumentLayout, but this seems not be possible without using private qt code ... ?

See also my post at https://bugreports.qt-project.org/browse/QTBUG-39796

Any ideas?
Many thanks, Christian