Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. "Smart" content for TextDocument with C++, QTextDocument, and QML TextEdit
Forum Updated to NodeBB v4.3 + New Features

"Smart" content for TextDocument with C++, QTextDocument, and QML TextEdit

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 343 Views
  • 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.
  • J Offline
    J Offline
    jtacoma
    wrote on last edited by
    #1

    context: I'm getting back into Qt after a very long time away because I'm working on an experimental hobby project that requires fine control of the document model behind a rich text editor: QML and Quick controls meet other requirements, like running on mobile devices and looking good.

    what I want: To connect a QML TextEdit to some C++ code that can automatically update the document, similar to QSyntaxHighlighter.

    what I've got: Something that basically works, which is great, but won't scale well. The documentation says I'm not allowed to change TextEdit::textDocument::textDocument directly, so I connect QTextDocument::contentChanged to a slot that copies and revises into a new QTextDocument before publishing toHtml() as the value of a property that is passed to TextEdit::setRichText().

    what's wrong: Creating a new QTextDocument and replacing the existing doc with an HTML representation a slightly different version of that doc, well, it just feels not-quite-right.

    question: Is there a more sensible way to do this? Is it any different from the way QSyntaxHighlighter should to be used with QML's TextEdit in order to have syntax highlighting while the user is editing the text?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jtacoma
      wrote on last edited by
      #2

      Aha! Given a TextEdit named textEdit, I can insert Qt's rich text HTML subset through textEdit.insert. In combination with textEdit.remove, effectively replacing existing text with re-formatted text. This is a (minimal) answer to my own question.

      Unfortunately, since I can't use a QTextCursor::beginEditBlock and endEditBlock with this solution, I can't prevent the remove+insert from adding two items to the QTextDocument's "undo" history. That's a bit messy, but I can live with it if I know I'll be able to fix it later - whether by being able to replace a document fragment in a single undoable op, or by overriding the built-in "undo/redo" feature with my own.

      I appreciate how the simplicity of a minimal API makes it easier to implement radical changes to the implementation of Quick Controls, and I love what's been accomplished in Qt's Quick Controls. Figuring out how to get what in this case is a bit of a puzzle, but as a beginner here I'm sure there are many interesting solutions I haven't thought of yet.

      1 Reply Last reply
      1

      • Login

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