字符串和变量组合 我是C++初学者,我有个问题。 如何在以下行中组合变量和字符串 QMessageBox::Question(this,"Report", "the Report is in Path: " + pdfPath + "saved, do you want to open it", QMessageBox::Yes | QMessageBox::No);

字符串和变量组合 我是C++初学者,我有个问题。 如何在以下行中组合变量和字符串 QMessageBox::Question(this,"Report", "the Report is in Path: " + pdfPath + "saved, do you want to open it", QMessageBox::Yes | QMessageBox::No);,c++,string,qt,qmessagebox,const-char,C++,String,Qt,Qmessagebox,Const Char,pdfPath是保存pdf文件路径的变量 我尝试了这个方法(“报告在路径中:“+pdfPath+”已保存,是否要打开它”),但它不起作用。 提前感谢:)如果pdf路径不是QString,则可以将其转换为QString // std::string pdfPath="C:\here!"; QMessageBox::question(this, "Report", "The Report is in Path: " + QString::

pdfPath
是保存pdf文件路径的变量

我尝试了这个方法(“报告在路径中:“+pdfPath+”已保存,是否要打开它”),但它不起作用。
提前感谢:)

如果pdf路径不是QString,则可以将其转换为QString

//
std::string pdfPath="C:\here!";
QMessageBox::question(this, "Report", "The Report is in Path: "  + QString::fromStdString(pdfPath) + "saved, do you want to open it", QMessageBox::Yes | QMessageBox::No);
//
串联QString的工作方式是严格正向的,因为运算符+重载

QString pdfPath="C:\here!";
QMessageBox::question(this, "Report", "The Report is in Path: "  + pdfPath + "saved, do you want to open it", QMessageBox::Yes | QMessageBox::No);

你说它不起作用是什么意思?请为二进制表达式(const Char[]和Char[])提供一个o无效操作数,这是错误。问题在于
“报告位于路径中:“
不是
QString
,它不能与
pdfPath
连接。”。您可能需要执行类似的操作:
QString(“报告保存在路径:%1中,是否打开”).arg(pdfPath)
。是的,它可以工作,谢谢@vahanchou您甚至可以使用:
tr(“报告保存在路径:%1中,是否打开它”),pdfPath)
来处理转换。