String + variable how?
Im a beginner learning Qt/C++ and I got into an error: I wanted to know how can I put a variable in this case "username" next to a string in t开发者_如何学运维he lines below.
QString username = ui->lineEdit->text();
QMessageBox msgBox;
msgBox.setText("Your username is: " VARIABLEHERE);
msgBox.exec();
So how to line it or should I use other function ? than msgBox.setText()
The nice Qt way is:
msgBox.setText(QString("Your username is: %1").arg(VARIABLEHERE));
For more information see QString::arg
If you want translation support:
msgBox.setText(tr("Your username is: %1").arg(VARIABLEHERE));
If you concatenate then all languages will have to use the same sentence semantics, and well... they can't.
msgBox.setText("Your username is: "+VARIABLEHERE);
i think + should help:
msgBox.setText("Your username is: " + username );
Related: When you debug using "std::cout" it works like this with QStrings:
cout << any_qstring.toUtf8().constData() << number_variable << endl;
Otherwise the compiler will tell you that "<<" is ambiguous.
Edit: it's even easier to call toStdString
cout << myString.toStdString() << some_int << endl;
And if you want to parse numbers from strings, use QString::number(your_double);
精彩评论