Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit and append

QTextEdit and append

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HennsWoerst
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HennsWoerst
      wrote on last edited by
      #2

      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);
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved