为跨平台文本编辑器应用程序更改 QTextStream 的 QT 行尾样式

Change QT End of Line Style of QTextStream for cross platform Text Editor application

所以我正在构建一个跨平台的 QT 文本编辑器。我正在开发 Linux,目标是 Windows。我正在让用户尽可能轻松地使用他们希望的任何编码保存文本文件。

我通过他们网站上的教程用 M Cross Environment "MXE" 编译了这个项目。

在 Linux 上,到目前为止一切都如我所愿地完美运行。

但是,在Windows,如果我用文本编辑器创建一个文件(无论我选择什么编码),然后用记事本打开它,整个文件都在一行中。

我相当确定我已经找到了问题所在。如果我打开我的 QT 文本编辑器创建的文件,Emacs 会说它有一个 "Unix" "end of line style."

我假设 Windows 不识别换行符,因为它们是 Unix 风格。但是,我不确定如何更改 QTextStream 中的 "end of line style"。该文档似乎没有任何帮助,而且我无法在 Stack overflow 或其他方面找到任何其他人有同样的问题。

以下是我保存文件的代码。 encodeString 变量控制保存文件的编码。

void mainwindow::on_actionSave_triggered()
{
    if (!saveFile.isEmpty())
    {
      //"saveFile" is populated with the file name when the file is first opened.
        QFile file(saveFile); 
        if (!file.open(QIODevice::WriteOnly))
        {
            qDebug() << "Could not find file."

        }
        else
        {
      //The following if statements will save the file in the proper encoding dependant on the value of "encodeString."
            QTextStream stream(&file)
            if (encodeString == "Plain Text")
            {
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-8")
            {
                stream.setCodec("UTF-8");
                stream.setGenerateByteOrderMark(true);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-16")
            {
                stream.setCodec("UTF-16");
                stream.setGenerateByteOrderMark(true);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-16BE")
            {
                stream.setCodec("UTF-16BE");
                stream.setGenerateByteOrderMark(true);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-32")
            {
                stream.setCodec("UTF-32");
                stream.setGenerateByteOrderMark(true);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-32BE")
            {
                stream.setCodec("UTF-32BE");
                stream.setGenerateByteOrderMark(true);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }


        }
    }


}

以下是打开文件的代码。它只是流式传输到 ui->textEdit 作为次要问题,我希望能够将正在打开的文件的编码记录到 encodeString 的值中。

void MainWindow::on_actionOpen_triggered()
{
  QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), tr("All Files (*.*);;Text Files (*.txt);;C++ Files (*.cpp *.h)"));  //Opens the QDialog for opening files.
  saveFile =fileName;       //Sets the value of "saveFile" to the value of "fileName." This insures that any time a "Save" function is performed, the file that the text is saved to, is the one that is intended.
    MainWindow::setWindowTitle(saveFile);
    if(!fileName.isEmpty()) // As long as the file name variable is not empty, this will trigger.
    {
        QFile file(fileName);
        if(!file.open(QIODevice::ReadOnly)) // If the file could not be opened, it will trigger this error message.
        {
            QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
                        return;
        }
        QTextStream in(&file);      //This sets the variable "in" to be the contents of the file.
    ui->textEdit->setText(in.readAll());    //This sets the text that is in the text edit field to be the contents of the file.
    file.close();       //This closes the file so that no memory errors are caused by having too many files open.
    }
}

老实说,我讨厌在这里回答我自己的问题,但实际上我能够在 QT 论坛上找到其他有同样问题的人。那就是这里 https://forum.qt.io/topic/42464/qplaintextedit-eats-line-endings 。使用此 post 中的信息并进行一些调整,我不仅能够使用正确的 Windows "line-ending" 保存文件,而且还能够使用正确的 Windows "line-ending" 保存文件编码。 QTextEdit 中的文本保存到 Qstring。然后使用 Qstring.replace 函数将结束于 \n 的行更改为 \r\n。然后设置 QTextStream 编码(如果是纯文本则不设置),并且 Qstring 流式传输到 QTextStream。然后 QTextStream 被发送到文件。 Qstring 的保存和操作在编码 if 语句之前完成,以减少所需的代码量。

这是完整的保存功能。

void MainWindow::on_actionSave_triggered()  // Saves the file
{
    if (!saveFile.isEmpty())
    {
        QString wSave;
        QFile file(saveFile);
        wSave = ui->textEdit->toPlainText();
        wSave.replace("\n", "\r\n");
        wSave.replace("\r\r\n", "\r\n");
        if (!file.open(QIODevice::WriteOnly))   // This if statement makes sure that the file can be written to.
        {
            // error message

        }
        else
        {
      QTextStream stream(&file);        // Prepares the file to receive the QTextStream
      if (encodeString == "Plain Text")     //The next set of "if" statements set the encoding that the file will be saved in. This is set by the combo box in the text editor.
            {

                qDebug() << wSave;
                stream << wSave;
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-8")
            {

                stream.setCodec("UTF-8");
                stream.setGenerateByteOrderMark(true);
                stream << wSave;
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-16")
            {
                stream.setCodec("UTF-16");
                stream.setGenerateByteOrderMark(true);
                stream << wSave;
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-16BE")
            {
                stream.setCodec("UTF-16BE");
                stream.setGenerateByteOrderMark(true);
                stream << wSave;
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-32")
            {
                stream.setCodec("UTF-32");
                stream.setGenerateByteOrderMark(true);
                stream << wSave;
                stream.flush();
                file.close();
            }

            if (encodeString == "UTF-32BE")
            {
                stream.setCodec("UTF-32BE");
                stream.setGenerateByteOrderMark(true);
                stream << wSave;
                stream.flush();
                file.close();
            }


        }
    }


}