PDA

View Full Version : QTextEdit to txt File



PatrickNev
25th June 2012, 10:46
Hi I've written small notepad. This is part of the code is responsible for saving text to some txt or doc file.

http://wklej.org/id/779292/

It's a slot. Of course the code works, the written text is saved to a txt file. But it is not my purpose. My simple notepad has a few functions about font manage. Here is the problem. The settings about text(font, size, color) aren't saved, only text. When I open my saved txt file, the text is right but the other settings like font or justify are forgot.

Sorry for my mistakes in english. Thanks in advance for your help.

wysota
25th June 2012, 11:37
Use toHtml() instead of toPlainText() if you want to preserve formatting. However then your file will not be a plain text file but rather an html file.

PatrickNev
25th June 2012, 12:21
Hmm... Yes I know this way with html. I'm thinking of use other "Text Editor" than QTextEdit. I've seen QPlainTextEdit but I don't know difference between this both text editors. So I aks otherwise, there is no way to save text with all properties to txt or doc file from QTextEdit ?

wysota
25th June 2012, 12:47
Maybe we don't understand each other. Plain text files do not contain any formatting data, only (as the name suggests) plain text. If you want to preserve formatting, you need to use a data format that supports it, like HTML, RTF, etc.

yeye_olive
25th June 2012, 12:54
The "txt" suffix is usually reserved for plain text files; as the name implies, this format represents plain text with no formatting information. You have to choose another format. wysota suggested html, you might look into rtf, odt (OpenDocument text), doc, etc. Qt most certainly does not have built-in support for doc, but might have for rtf or odt. Unfortunately the online docs seem to be down for the moment (or I might have connection issues on my side), so I cannot check that for you.

wysota
25th June 2012, 12:58
One can always use this: http://doc.qt.nokia.com/qq/qq27-odfwriter.html

However there are no guarantees that a particular formatting is going to be preserved and there is no equivalent reader class available so you won't be able to read the file back in.

PatrickNev
25th June 2012, 13:03
I mean I want to do notepad like this from microsoft for example. There is possibility write some text, set font and save it all. Then when I open this saved txt file everything is as I set.
From this it follows that the "Microsoft's Notepad" with every new txt file, create somewhere data files and there saved information about fonts etc. Am I understand this right ?

Btw, I don't really waste your time. I only ask what code you would use if you wanted save text to txt or doc file with font, size etc.

wysota
25th June 2012, 13:11
I mean I want to do notepad like this from microsoft for example. There is possibility write some text, set font and save it all. Then when I open this saved txt file everything is as I set.
No, not really. Setting a font in MS Notepad sets the font for the program and not for that file. Try saving two different files with two different fonts and then load them back in and see if the fonts adjust accordingly.