windows 新行未写入文件(Qt)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7104328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:53:32  来源:igfitidea点击:

New Line not being written on file (Qt)

windowsvisual-studio-2010qt

提问by Maxes

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().

我正在使用 qtextedit 在我的软件中记录活动日志。稍后当我想使用 toPlainText() 将其保存为文本时,生成的文本文件是一行,没有任何换行符。我从plainText() 开始记录并使用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();
}

}

}

采纳答案by Tim Meyer

From the QTextStream class reference (that line is a bit hidden):

从 QTextStream 类参考(该行有点隐藏):

Note: On Windows, all '\n' characters are written as '\r\n' if QTextStream's device or string is opened using the QIODevice::Text flag.

注意:在 Windows 上,如果使用 QIODevice::Text 标志打开 QTextStream 的设备或字符串,则所有 '\n' 字符都写为 '\r\n'。

where '\n' is the UNIX line ending and '\r\n' is the Windows line ending (CR/LF).

其中 '\n' 是 UNIX 行结尾,'\r\n' 是 Windows 行结尾 (CR/LF)。

Remove the QTextStream initialisation at the start of your method and change the if statement to this:

在方法开始时删除 QTextStream 初始化并将 if 语句更改为:

if (!logfName.isEmpty())
{
    logFile.setFileName(logfName);
    logFile.open(QIODevice::WriteOnly);
    QTextStream logOut(&logFile, QIODevice::Text);
    logOut<<ui.statusReport->toPlainText();
    logFile.close(); 
} 

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

还要注意我是如何改变 if 条件的。logfName 默认设置为 "",我不确定与 NULL 比较是否有效。你最好使用 QString::isEmpty() 函数

回答by Frank Osterfeld

Probably the file is written using UNIX line endings? You should open the file in text, to get the local (Windows) line-endings:

该文件可能是使用 UNIX 行结尾编写的?您应该以文本形式打开文件,以获取本地 (Windows) 行结尾:

  logFile.open(QIODevice::WriteOnly|QIODevice::Text);

回答by GTRONICK

I had the same issue. However, the solution given by @Tim Meyer, did not work for me. In the line:

我遇到过同样的问题。但是,@Tim Meyer 给出的解决方案对我不起作用。在行中:

QTextStream logOut(&logFile, QIODevice::Text);

An error is shown. So, I did the following and worked for QT5.8:

显示错误。所以,我做了以下工作并为 QT5.8 工作:

/**
* Method to save a text file.
* @param asFileName: Complete file path, including name and extension.
* @param asText: Text to be written in the file
* @return true if the save was successful, false otherwise.
*/
bool MainWindow::saveFile(QString asFileName, QString asText)
{
    QFile file(asFileName);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QMessageBox::critical(this,"Error","File could not be opened");
        return false;
    }

    QTextStream out(&file);
    out << asText;
    file.close();

    return true;
}