PDA

View Full Version : Saving rich text to XML file



been_1990
31st July 2010, 17:30
How can I save a QTextEdit's text and preserve it's current formating?

tbscope
1st August 2010, 14:53
Since a subset of html formatting is used, you can simply save the text as html:
http://doc.qt.nokia.com/4.6/qtextedit.html#html-prop

been_1990
1st August 2010, 18:09
Thanks for the reply.
I know(can't implement it ..) that I can save the QTextEdit's QTextDocument formatting to a QTextFormat.toListFormat (). But the I would have to save the plain text and the formating info to the xml file.
Saving text to html does seem to be easier, but is it better?

ChrisW67
1st August 2010, 23:17
Better for what purpose? Your stated aim of saving the content and formatting of a QTextEdit is met by saving the result of QTextEdit::toHtml() and restoring it using QTextEdit::setHtml(). If you want to put that inside another XML document element then you probably want to insert it as a CDATA block.

been_1990
2nd August 2010, 15:22
I mean "what's the recommended way?", if there is such a thing. There's always a way that will be better, more flexible, reliable,etc..

ChrisW67
2nd August 2010, 23:46
I think the HTML option is the most straightforward. I'm not certain what it does with embedded images, like Text Object Example, so you might have to experiment if that is your situation. Worst case is having to go to the underlying QTextDocument to develop a serialisation method based on the blocks structure.