Copying text from QTextEdit to .odt file and retain formatting
-
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 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.
-
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 theQTextCursor
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 ofQTextEdit::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 aQTextCursor
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:- Iterate over
QTextBlock
s in selection, copy them into a temporary sideQTextDocument
(taking care to strip unselected edges) - For each block, iterate over its' special formats and explicitly apply them on the side document.
- Export the side document as HTML or ODT.
You might possibly find an easier approach.
- The