Results 1 to 5 of 5

Thread: Writing the text within a text edit to a file

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Writing the text within a text edit to a file

    Hi all I have been trying to write to a text file. The code i currently have is:

    Qt Code:
    1. QString savelocation = ui->ComboBox->text();
    2. QFile ffile(savelocation + "/" + ui->lineEdit->text() + ".txt");
    3. if (ffile.open(QFile::WriteOnly)) {
    4. QTextStream out(&ffile);
    5. out << ui->textEdit->toPlainText();
    6. }
    To copy to clipboard, switch view to plain text mode 


    The data is being written to the file fine. However there are no return characters between the lines;
    e.g. if my text edit held:

    Qt Code:
    1. The Quick brown
    2. Fox jumped over
    3. The lazy dog
    To copy to clipboard, switch view to plain text mode 

    it would be written to the file like this:

    Qt Code:
    1. The Quick brown Fox jumped over The lazy dog
    To copy to clipboard, switch view to plain text mode 

    How do i solve this problem

    Thanks for your time and trouble

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Writing the text within a text edit to a file

    I believe you can turn line wrap (or wordwrap policy) off, forcing you to use line ends.

    Also look at QTextDocument::textWidth(). You can use an underlying QTextDocument with QTextEdit.
    Last edited by schnitzel; 23rd February 2011 at 22:45.

  3. #3
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing the text within a text edit to a file

    I tried the turning off the line wrap. It didn't work. I want to save my file as a text file. could ypu perhaps give me some example code?

    Thanks

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Writing the text within a text edit to a file

    You may try using QIODevice::Text flag when opening the file:
    Qt Code:
    1. ffile.open(QIODevice::WriteOnly | QIODevice::Text)
    To copy to clipboard, switch view to plain text mode 
    From the docs (about QIODevice::Text):
    When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

  5. The following user says thank you to stampede for this useful post:

    Splatify (23rd February 2011)

  6. #5
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing the text within a text edit to a file

    Thanks stampede!!! Works a treat

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 20:06
  2. writing to a text-file
    By QtBros61 in forum Newbie
    Replies: 7
    Last Post: 9th April 2010, 12:15
  3. Replies: 1
    Last Post: 3rd September 2008, 15:16
  4. Qt class showing text area/text edit
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 21st August 2008, 13:15
  5. Finding text on Text edit
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2006, 14:20

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.