How to implement QTextDocument serialization

2019-09-15 02:35发布

问题:

This question I have asked before and just got answer that there is an open bug for this. But this is a really required feature and, I guess, each Qt programmer who programmes a more or less serious application, it is quite probable that there is used a QTextEdit and the data is inserted in QTextEdit is serialized and deserialized. Thus I consider this issue very importand and it can be useful for many Qt developers. So I have decided to discuss with good programmers how to implement operator<<(QDataStream, QTextDocument). Otherwise we should wait when Qt guys will be kind to do that by themselves :).

So say in a QTextEdit I have some pictures and text. In different fragments the text has different format. How to get the content of this kind of QTextEdit and how to serialize, in order to show then as it was?

EDIT: I know that I can get the formated text and images by using documnet() method, which returns QTextDocument. I also know that QTextDocument has no operator<<(QDataStream, QTextDocument) and there is a request for writting this operator but it is still open. So please help me to write this method. I guess I should inherit the QTextDocument class, then to add a new member (a container) that stores all the names of resources that are present in the QTextEdit. Then I should serialize all resources by iterating on them by using the container I have defined. Is it a good solution? If yes, then could you please help me to implement? If no then please provide me with a better idea.

回答1:

You already asked this question in April:

Qt - serialize/deserialaize text and picture

The answers are still the same.

  1. Wait for Qt to fix QTBUG-9258
  2. Perform the serialization yourself

Depending on your needs a quick and dirty way to do it would be to get the HTML representation of the contents with QTextEdit::toHtml, then base64 encode it. I'm not sure how this will work with pictures.

Please don't repost the same questions.



回答2:

See here:

How to serialize and deserialize rich text in QTextEdit?



标签: c++ qt qtextedit