Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Forgot how to display a number within a QString....



  • Yea, the title says it all, anyways, i haven't used QT or QString in awhile so i tried this: @ QString str = QString("%d").arg(attempts);
    ui.numTries->setText(str);
    @

    and what i got was the literal "%d", so idk



  • @
    QString str1 = QString::fromNumber(attempts);
    // or
    QString str2 = QString("%1").arg(attempts);
    @



  • ah thank you



  • Or, use the %n in your string instead of %d. This one is very useful in cases where you need two or more different strings based on the value:

    @
    QString attemptsStr = tr("%n attempts").arg(attempts);
    @

    In your translation, you will be able to create different translations for your string based on singular or plural, and for some languages even more than those two (some languages have more than one plural form).


Log in to reply