PDA

View Full Version : Writing the text within a text edit to a file



Splatify
23rd February 2011, 19:54
Hi all I have been trying to write to a text file. The code i currently have is:


QString savelocation = ui->ComboBox->text();
QFile ffile(savelocation + "/" + ui->lineEdit->text() + ".txt");
if (ffile.open(QFile::WriteOnly)) {
QTextStream out(&ffile);
out << ui->textEdit->toPlainText();
}


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:


The Quick brown
Fox jumped over
The lazy dog

it would be written to the file like this:


The Quick brown Fox jumped over The lazy dog

How do i solve this problem

Thanks for your time and trouble

schnitzel
23rd February 2011, 21:39
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.

Splatify
23rd February 2011, 22:04
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

stampede
23rd February 2011, 22:42
You may try using QIODevice::Text flag when opening the file:

ffile.open(QIODevice::WriteOnly | QIODevice::Text)
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.

Splatify
23rd February 2011, 22:48
Thanks stampede!!! Works a treat :D