i am keeping a log of activities in my software using qtextedit. later when i want to save it as a text using toPlainText(), the resulting text file is a single line without any line breaks.
I start logging by plainText() and add subsequent additions using append().
void rocketscience::saveLog(){
QFile logFile;
QTextStream logOut;
QString logfName;
QSettings prevSet("us","fr");
if (defaultDir.exists(prevSet.value("settings/logPath").toString()))
logfName= QFileDialog::getSaveFileName(this,"Save File",fName,"Text (*.txt");
if (logfName!=NULL){
logFile.setFileName(logfName);
logFile.open(QIODevice::WriteOnly);
logOut.setDevice(&logFile);
logOut<<ui.statusReport->toPlainText();
logFile.close();
}
}
From the QTextStream class reference (that line is a bit hidden):
where ‘\n’ is the UNIX line ending and ‘\r\n’ is the Windows line ending (CR/LF).
Remove the QTextStream initialisation at the start of your method and change the if statement to this:
Also note how I changed the if condition. logfName is by default set to “”, I’m not sure if comparing to NULL will work. You’re better off using the QString::isEmpty() function