Results 1 to 8 of 8

Thread: QTextEdit to txt File

  1. #1
    Join Date
    Jun 2012
    Posts
    3
    Qt products
    Qt4

    Default QTextEdit to txt File

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit to txt File

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    3
    Qt products
    Qt4

    Default Re: QTextEdit to txt File

    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 ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit to txt File

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit to txt File

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit to txt File

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2012
    Posts
    3
    Qt products
    Qt4

    Default Re: QTextEdit to txt File

    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.
    Last edited by PatrickNev; 25th June 2012 at 13:12.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit to txt File

    Quote Originally Posted by PatrickNev View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. save contents QTextEdit in a .txt file
    By giorgik in forum Newbie
    Replies: 2
    Last Post: 31st May 2012, 10:11
  2. Binary file in QTextEdit
    By xproch13 in forum Qt Programming
    Replies: 3
    Last Post: 28th October 2010, 20:07
  3. Display pdf file in QTextEdit
    By toutarrive in forum Qt Programming
    Replies: 8
    Last Post: 7th August 2009, 16:51
  4. Play gif file in QTextEdit field
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 15:28
  5. reading QTextEdit from file
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 29th March 2006, 21:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.