1

i m a beginner in c++ and i have a question. how can i combine a variable and a string in the folowing line.

QMessageBox::Question(this,"Report", "the Report is in Path: "  + pdfPath + "saved, do you want to open it", QMessageBox::Yes | QMessageBox::No);

pdfPathis my Variable where the path of my pdf-file is saved.

I tried this, ("the Report is in Path: " + pdfPath + "saved, do you want to open it") but it does not work. Thank you in advance :)

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Boumo
  • 19
  • 2
  • What do you mean by *it does not work*? Please provide a [mre] – Damien Jan 20 '21 at 08:54
  • o invalid operands to binary expression (const Char[] and char[]) that is the error – Boumo Jan 20 '21 at 08:56
  • 1
    The problem is that `"the Report is in Path: "` is not a `QString` and it cannot be concantenated with `pdfPath`. You might want to do something like this instead: `QString("the Report is in Path: %1 saved, do you want to open it").arg(pdfPath)`. – vahancho Jan 20 '21 at 08:56
  • You might even go: `tr("the Report is in Path: %1 saved, do you want to open it", pdfPath)` to handle translation. – Jarod42 Jan 20 '21 at 10:30

1 Answers1

1

if pdf path is not a QString then you can to convert it into one,

//
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);
//

concatenating QStrings works straigh forward cos the operator + is overloaded

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);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97