C++ QString 换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27620038/
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-08-28 20:49:56  来源:igfitidea点击:

QString New Line

c++qtqstringqtcore

提问by Root0x

I want to add a new line to my QString. I tried to use \n, but I am receiving an error of "Expected Expression". An example of my code can be found below:

我想在我的QString. 我尝试使用\n,但收到“预期表达式”错误。可以在下面找到我的代码示例:

if (ui->lineEdit_Company_Name->text().isEmpty())
    ErrorLog = ErrorLog + "Company Name is empty", \r\n;
if(ui->lineEdit_Company_Owner->text().isEmpty())
    ErrorLog = ErrorLog + "Company Owner is empty", \r\n;

回答by lpapp

You need to use operator+, push_back, appendor some other means for appending when using std::string, QStringand the like. Comma (',') is not a concatenation character. Therefore, write this:

您需要在使用时使用operator+push_backappend或其他一些附加方法std::stringQString等等。逗号 (',') 不是连接字符。因此,写这个:

if (ui->lineEdit_Company_Name->text().isEmpty())
    ErrorLog = ErrorLog + "Company Name is empty\n";
if(ui->lineEdit_Company_Owner->text().isEmpty())
    ErrorLog = ErrorLog + "Company Owner is empty\n";

Please also note that \nis enough in this context to figure out the platform dependent line end for files, GUI controls and the like if needed. Qt will go through the regular standard means, APIs or if needed, it will solve it on its own.

另请注意,\n如果需要,在此上下文中足以确定文件、GUI 控件等的平台相关行结束。Qt 将通过常规的标准方法、API,或者如果需要,它会自行解决。

To be fair, you could simplify it even further:

公平地说,您可以进一步简化它

if (ui->lineEdit_Company_Name->text().isEmpty())
    ErrorLog += "Company Name is empty\n";
    // or ErrorLog.append("Company Name is empty\n");
    // or ErrorLog.push_back("Company Name is empty\n");
if(ui->lineEdit_Company_Owner->text().isEmpty())
    ErrorLog += "Company Owner is empty\n";
    // or ErrorLog.append("Company Owner is empty\n");
    // or ErrorLog.push_back("Company Owner is empty\n");

Practically speaking, when you use constant string, it is worth considering the use of QStringLiteralas it builds the string compilation-time if the compiler supports the corresponding C++11 feature.

实际上,当您使用常量字符串时,如果编译器支持相应的 C++11 特性,则值得考虑使用QStringLiteral因为它构建字符串编译时。

回答by Jonathan Mee

I would concur with lpappthat you should simply incorporate the '\n'into the string literal you are appending. So:

我同意lpapp 的观点,您应该简单地将 合并'\n'到您要附加的字符串文字中。所以:

if (ui->lineEdit_Company_Name->text().isEmpty()){
    ErrorLog += "Company Name is empty\n";
}
if(ui->lineEdit_Company_Owner->text().isEmpty()){
    ErrorLog += "Company Owner is empty\n";
}

But I'd like to also mention that the not just Qt, but C++ in general translates, '\n'to the correct line ending for your platform. See this link for more info: http://en.wikipedia.org/wiki/Newline#In_programming_languages

但我还想提一下,不仅 Qt,而且 C++ 通常都会转换'\n'为适合您平台的正确行尾。有关更多信息,请参阅此链接:http: //en.wikipedia.org/wiki/Newline#In_programming_languages

回答by S.M.Mousavi

Also you can use endlas follow:

您也可以使用endl如下:

ErrorLog += "Company Name is empty" + endl;