I'm using pyside6's qtextedit to write text and images. And in this state, I want to copy the content to the clipboard and insert it into ms-word.
The content of qtextedit consists of several texts and images.

I created qtextedit content as above. And the above is a photo captured using Windows Capture.

The photo above is the content I Ctrl+C pasted into MS-word. As shown above, the images are not visible at all.
aosdijfioas
   asiufisuh
The above content is the same as ctrl+c in qtextedit and pasted into stackoverflow with ctrl+v. "" .
I don't know what this is, but maybe I need to do something when inserting an image into qtextedit or copying it with ctrl+c. How can I use it in another editor?
Currently, qtextedit is in its default state created by attaching it from pyside-designer.
Honestly, I don't know what to do because I didn't expect this to happen. I used Ctrl+C, but I didn't know that the image wouldn't be copied.
Rich text content, similarly to HTML, normally uses references for embedded objects like images: the object is not physically stored within the document, but only a reference (usually, a local or remote address) to it.
This is also reflected when using the OS clipboard, and for good reasons: imagine copying a text selection that contains dozens of images that are just references to local files: image data takes a lot of memory, and it's quite easy to risk "out-of-memory" issues if you try to copy them.
Qt doesn't provide a direct way to do this, since it's a potentially harmful procedure; still, the requirement is valid and acceptable.
Since cross-platform clipboard operations of rich text is normally done using HTML, the solution is to use the Base64 encoding that HTML also supports: image data can be stored using standard ASCII characters that are used to encode the binary data of the image. In that case, the images are not references anymore: they are embedded within the document as raw data and their address is a memory representation of the file data.
The trick is to properly manage the operation while copying the selection to the clipboard.
Luckily, QTextEdit provides the
createMimeDataFromSelection()function: since it's "virtual", it means that it can be overridden in Python and will always be called when the user attempts to create a copyable selection.Consider that QTextDocument uses a special subclass to display images: QTextImageFormat (which inherits QTextCharFormat and QTextFormat) is virtually a "character" in the document, that is eventually "converted" to a displayed image.
The process uses the following steps:
toHtml()function anyway, so that conversion is not an issue);name()of QTextImageFormat or theImageNameproperty of an uncast QTextCharFormat is similar to thesrc="..."attribute of an HTML<img>tag) and if it actually is a valid image;In reality, while the above seems very complex, it can be achieved quite easily, as long as one knows what to do:
Now, remember the memory issue above. Even if modern computers normally have enough RAM, converting lots of huge images into ASCII encoded strings is not always a good idea.
The above code works quite fine for very simple cases (a few, relatively small images), but it could create issues for big data sizes, like lots of images or uncompressed ones. So, use it with care, and eventually consider some failsafe system, like basic alerts or pre-processing file sizes before actually proceeding with clipboard copying.
Finally, be aware that all this will only work for relatively modern programs that properly support HTML based clipboard and embedded images: rich text clipboard is not an absolute certainty, and it may not work as expected, so you must be aware of that and also consider to eventually alert the user about the consequences. Conversion to base64 always requires more memory as opposed to the original data size, and support by other programs cannot be taken as granted.
Also, using clipboard within the same QTextEdit as done above will make any copy/pasted image as embedded, even if done within the same widget, so you should consider some further checking, possibly by creating an internal "clipboard system" that would eventually paste real QTextDocument fragments instead of the HTML provided by the overall clipboard.