ibanezz
ibanezz

Reputation: 95

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 the 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()

Upvotes: 0

Views: 3552

Answers (5)

KYL3R
KYL3R

Reputation: 4073

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

Upvotes: 0

Adi
Adi

Reputation: 2061

i think + should help: msgBox.setText("Your username is: " + username );

Upvotes: 1

Tom Kerr
Tom Kerr

Reputation: 10720

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.

Upvotes: 5

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

The nice Qt way is:

msgBox.setText(QString("Your username is: %1").arg(VARIABLEHERE));

For more information see QString::arg

Upvotes: 10

Paul
Paul

Reputation: 141877

msgBox.setText("Your username is: "+VARIABLEHERE);

Upvotes: 3

Related Questions