Qt - serialize/deserialaize text and picture

2019-06-14 09:11发布

问题:

I want to use a field (kind of QTextEdit), which is capabale of storing picture and text (like MS Word is doing) and it serializes and deserialaizes picture+text data. Is there any Qt Widget that allows us to manipulate with picture and text simultaniously and it has set/get functions which operate with serializable type? In othger words, I want to know if there exsists any Qt widget that can store picture+text and has "get" types of function that returns the content of that widgets editable area, which is a Type that could be serialized with QDataStream.

回答1:

I would create a data structure that contains the text and picture for you. You can then use that data structure (or object) within your display widget. This allows you to serialize your data without trying to serialize the widget, which is probably not what you want.

For serialization, I'd recommend you take a look at QDataStream.

The following posts provide some details about QDataStream:

Serialization with Qt

Overloading the QDataStream << and >> operators for a user-defined type

Reading/Writing QObject



回答2:

I dont think there is something that will satisfy all your needs.

To display text + images you could use QTextEdit, that is capable of displaying a pretty rich subset of HTML. Basically QTextEdit allows you to read and edit QTextDocument object, that is essentially what you want to serialize and deserialize.

But as far as I know there is no default implementation of serialization for QTextDocument. I saw a request for this feature, but who knows when it will be implemented: http://bugreports.qt-project.org/browse/QTBUG-9258

Edit 1: Implementing serialization/deserialization of QTextDocument should not be way to complicated, because all the building blocks of QTextDocument are serializable. Images are stored in QTextDocument and are available as QVariants, html itself is just a string, so it should not be too complicated to wrap everything together.



回答3:

See this:

How to serialize and deserialize rich text in QTextEdit?