如何实现QTextDocument序列化

发布于 2024-09-10 08:05:54 字数 631 浏览 8 评论 0原文

我之前问过这个问题,刚刚得到的答案是存在一个未解决的错误。但这确实是一个必需的功能,我想,每个编写或多或少严肃的应用程序的 Qt 程序员很可能使用过 QTextEdit,并且插入到 QTextEdit 中的数据被序列化和反序列化。因此我认为这个问题非常重要,对于许多 Qt 开发人员来说可能很有用。所以我决定和优秀的程序员讨论一下如何实现operator<<(QDataStream, QTextDocument)。否则我们应该等待 Qt 人员愿意自己做这件事:)。

假设在 QTextEdit 中我有一些图片和文本。在不同的片段中,文本具有不同的格式。如何获取这种QTextEdit的内容以及如何序列化,以便按原样显示?

编辑:我知道我可以使用 documnet() 方法获取格式化的文本和图像,该方法返回 QTextDocument。我还知道 QTextDocument 没有运算符<<(QDataStream, QTextDocument) 并且有写入此运算符的请求,但它仍然处于打开状态。所以请帮我写这个方法。 我想我应该继承 QTextDocument 类,然后添加一个新成员(容器)来存储 QTextEdit 中存在的所有资源名称。然后,我应该使用我定义的容器对所有资源进行迭代来序列化所有资源。这是一个好的解决方案吗?如果是的话,你能帮我实现吗?如果没有,请给我一个更好的主意。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

壹場煙雨 2024-09-17 08:05:54

您已经在四月份提出了这个问题:

Qt - 序列化/反序列化文本和图片

答案还是一样。

  1. 等待 Qt 修复 QTBUG-9258
  2. 自己执行序列化

根据您的需要,一种快速而肮脏的方法是获取内容的 HTML 表示形式 QTextEdit::toHtml,然后base64编码。我不确定这将如何处理图片。

请不要转发相同的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文