PDA

View Full Version : Get text from QTextEdit with CR LF



arcull
9th September 2015, 07:08
Hi. I'm using QTextEdit to append some logs while my gui app is running. On closing it I want to append the content this to a log file. I do something like below:


QDateTime cdt = QDateTime::currentDateTime();
const QString LogFileName = QApplication::applicationDirPath() +
"/" + cdt.toString("yyyy-MM-dd_hh-mm-ss") + "_log.txt";

QFile LogFile(LogFileName);
LogFile.open(QIODevice::WriteOnly);
QTextStream outstr(&LogFile);
outstr << ui->textEdit->toPlainText();
LogFile.close();

The content of the QTextEdit is written to my file, but I would like it with CR and LF just like it is displayed in QTextEdit. How can I do that, much thanks.

Added after 4 minutes:

Sorry, posted to early. The text is written with new lines just as it should be. I opened the file with windows notepad, which just displayed it wrong.

anda_skoa
9th September 2015, 08:59
Windows notepad is a very old application, it can only deal with so-called "Windows line endings".

Btw, very bad choice of base path, this is a read-only location once you install the application.

Cheers,
_

arcull
9th September 2015, 10:28
Btw, very bad choice of base path, this is a read-only location once you install the application.
Cheers,
_
Yes you are right, even though I thought this app will be some kind of portable version, so there will be no installer putting this under "C:\Program Files\...", but despite that maybe it's better to put is somewhere in current user folder. How would I get then this path by the way? Do you have some short sample code? Thanks again.

anda_skoa
9th September 2015, 10:52
Try QStandardPaths::writableLocation() with maybe DataLocation.

Cheers,
_

arcull
9th September 2015, 13:35
According to the docs, QStandardPaths::AppDataLocation looks the right one, but is missing in class, however I can get documents location successfully. What am I missing?


QStringList sps = QStandardPaths::standardLocations(QStandardPaths:: DocumentsLocation);
foreach (QString s,sps)
qDebug() << s;

anda_skoa
9th September 2015, 14:40
Are you on Qt5.4 or later?

According to the docs AppDataLocation was added at 5.4

Cheers,
_

arcull
9th September 2015, 16:25
Ahh grrrr, I'm on 5.1. What does it take to upgrade to 5.5, just reinstall or is it more complicated?

anda_skoa
9th September 2015, 16:36
Ahh grrrr, I'm on 5.1.

Could still use the data location and create a subdir with your application name.



What does it take to upgrade to 5.5, just reinstall or is it more complicated?
No, that should be all that it takes.

Cheers,
_

arcull
9th September 2015, 17:12
Will use data location instead of reinstall, thanks.

Rondog
9th September 2015, 19:44
I think the problem is with QTextStream. If you didn't use this you would end up with something closer to what is displayed in QTextEdit



QTextEdit *text_edit;
...
QString text_data = text_edit->toPlainText();

QFile file(output_file_name);
file.write(text_data.toLatin1());
..
file.close();

arcull
10th September 2015, 05:39
I think the problem is with QTextStream. If you didn't use this you would end up with something closer to what is displayed in QTextEdit



QTextEdit *text_edit;
...
QString text_data = text_edit->toPlainText();

QFile file(output_file_name);
file.write(text_data.toLatin1());
..
file.close();

Thanks, but it was not the problem of code page. It was the windows notepad which just displays data some weird way. If I open the file with Notepad++ for instance it is ok.

Rondog
10th September 2015, 12:48
Notepad does not display files properly unless they have the \r\n line ending. It has always done this. No other editor has this problem (including Wordpad).

When you open the file you can add the 'text' flag and Qt will convert to the proper line terminator for you based on the OS. If it sees '\n' in the file data to write it will automatically be converted to '\r\n'.



QFile file.open(QIODevice::WriteOnly | QIODevice::Text);


Optionally you could use QString::replace() to do this as well. I should be wrapped with an #ifdef OS preprocessor and (very important) you must make sure your input data does not already have \r\n for line terminators (it will become \r\r\n after 'replace').



#ifdef Q_OS_WIN
text_data.replace('\n','\r\n');
#endif
.