QTextEdit and append



  • Hi!

    I have a problem I've tried to solve all day but to no avail as it seems. I am trying to insert lines of HTML code to a QTextEdit (with append()), the only problem is, that every time I do this a new line is started since a new QTextBlock is created. I do not want this behaviour since this destroys my formatting, I want to set my linebreaks myself.

    Now I've looked for a solution and found insertHtml(). While this does exactly what I want there is a downside: the blockcount is not updatet. But I need this because I have to access the added strings later by their line/blocknumber. I have found no possibility to manually set the number of blocks (or the lines for that matter) and have honestly no idea what I could do next.

    So the bottom line is, that I want the behaviour of insertHtml() but that every time I call this method the blockcount of the underlying QTextDocument is updatet (it would be fine to do it manually, I just do not know how to accomplish this?)

    Thank you!



  • I think I found a way, but I have to test this further:

    @
    QTextCursor cursor = edit->textCursor();
    cursor.insertBlock();
    cursor.insertHtml("<strong> Line1 </strong>");
    cursor.insertHtml("<strong> Line2 </strong>");
    cursor.block().setLineCount(2);
    @

    This displays the two Strings in one line, blockcount is 1 and linecount is 2. If you want to jump to Line2 you can do this with

    @
    QTextBlock textBlock = edit->document()->findBlockByLineNumber(2);
    QTextCursor textCursor = edit->textCursor();
    textCursor.setPosition(textBlock.position());
    edit->setTextCursor(textCursor);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.