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. Copying text from QTextEdit to .odt file and retain formatting
Forum Updated to NodeBB v4.3 + New Features

Copying text from QTextEdit to .odt file and retain formatting

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 371 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.
  • A Offline
    A Offline
    ACBlue
    wrote on last edited by ACBlue
    #1

    I have written a simple DIY code editor based on QTextEdit and am now adding features to it. I have basic syntax highlighting which changes the color of text etc. Now I want to copy this text to a .odt file. I am only able to paste the text as unformatted plain text. All the formatting information gets lost. I tried creating a MIME object from the selection and noticed that there is no formatting information in it.

    I can copy the text from one QTextEdit to another QTextEdit and the formatting is retained. I would like to copy the text to an application outside Qt. I am guessing that I am missing a very basic setting to make sure that my selection also selects the formatting information. Ideally, I would like to copy the text as is (font, size and color) to a .odt file.

    Is this possible? Any help would be greatly appreciated.

    I 1 Reply Last reply
    0
    • A ACBlue

      I have written a simple DIY code editor based on QTextEdit and am now adding features to it. I have basic syntax highlighting which changes the color of text etc. Now I want to copy this text to a .odt file. I am only able to paste the text as unformatted plain text. All the formatting information gets lost. I tried creating a MIME object from the selection and noticed that there is no formatting information in it.

      I can copy the text from one QTextEdit to another QTextEdit and the formatting is retained. I would like to copy the text to an application outside Qt. I am guessing that I am missing a very basic setting to make sure that my selection also selects the formatting information. Ideally, I would like to copy the text as is (font, size and color) to a .odt file.

      Is this possible? Any help would be greatly appreciated.

      I Offline
      I Offline
      IgKh
      wrote on last edited by IgKh
      #2

      @ACBlue

      Welcome to the forum. Just to confirm, you are doing the syntax highlighting with QSyntaxHighlighter, right? Not by directly changing character formats in the document through the QTextCursor interface.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        ACBlue
        wrote on last edited by
        #3

        Yes, that is correct.

        I 1 Reply Last reply
        0
        • A ACBlue

          Yes, that is correct.

          I Offline
          I Offline
          IgKh
          wrote on last edited by
          #4

          @ACBlue

          OK, unfortunately it is not a matter of missing a "very basic setting". As a little background, there are three possible sources for character formats that affect what you see on screen when using QTextEdit:

          • The QTextDocument's internal format collection, affected only by explicit editing actions or programmatically using the QTextCursor API and covered by the undo/redo functionality.
          • "Special" formats that are stored for each text block separately in its' associated layout object, accessible through the QTextBlock::layout method. While indirectly accessible through the document, they are not part of its' data model.
          • Additional formats that are set by the editor widget itself, e.g the automatic highlighting you get out of the box for selected text as well as anything you set yourself via QTextEdit::setExtraSelections.

          The layer in Qt responsible for text layout and rendering combines the three sources.

          The problem for your use case, is that formats set by QSyntaxHighlighter are of the second kind - only weakly associated with the document (as they are derived from content, and are not user editing actions). On the other hand, the default implementation of QTextEdit::createMimeDataFromSelection (as well as anything else that serializes the document) only looks at formats of the first kind. You should be able to confirm this by programmatically inserting a bit of styled text via a QTextCursor and see that it is copied along with the format.

          Basically, you'll need to re-implement QTextEdit::createMimeDataFromSelection to manually create MIME data that is aware of the special formats. One possible approach can be:

          1. Iterate over QTextBlocks in selection, copy them into a temporary side QTextDocument (taking care to strip unselected edges)
          2. For each block, iterate over its' special formats and explicitly apply them on the side document.
          3. Export the side document as HTML or ODT.

          You might possibly find an easier approach.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ACBlue
            wrote on last edited by
            #5

            Thanks for the clarification. I wanted to copy the code directly from my editor to generate some documentation. I think I will instead use a proper documentation system and let it handle the highlighting.

            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