Solved How to set a int variable in a QLabel next to normal text?
-
Hey,
I want to place a variable next to plain text.
ui->MessageLabel->setText("You have lost! :(\n The secret number was: ");
I know about these two statements (setNum and convert):
ui->MessageLabel->setNum(secretNumber); qDebug() << QString::number(secretNumber)
But i don't know gow to combine it with the text "You have lost! :(\n The secret number was: " already extant in the QLabel.
Thanks in advance!
-
The best solution is:
ui->MessageLabel->setText( tr("You have lost! :(\n The secret number was: %1").arg(secretNumber));
because the
tr()
call also makes your text translatable. -
Allright thanks! 🙂
-
@DevinQT please mark this topic as SOLVED if you are satisfied :)
-
Allright will do. Maybe it's helpful for others to list the other methods?
-
Hi
One could also do
ui->MessageLabel->setText("You have lost! :(\n The secret number was: " + QString::number(secretNumber) );
but aha_1980 solution is much nicer. -
I guess there are endless. e.g.:
QString text = QString("Hello ") + QString::number(123);
or
QString text("Hello "); text.append(QString::number(123));
etc.