Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set a int variable in a QLabel next to normal text?
Forum Updated to NodeBB v4.3 + New Features

How to set a int variable in a QLabel next to normal text?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DevinQT
    wrote on last edited by DevinQT
    #1

    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!

    aha_1980A 1 Reply Last reply
    0
    • D DevinQT

      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!

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      @DevinQT

      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.

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • D Offline
        D Offline
        DevinQT
        wrote on last edited by
        #3

        Allright thanks! 🙂

        aha_1980A 1 Reply Last reply
        1
        • D DevinQT

          Allright thanks! 🙂

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @DevinQT please mark this topic as SOLVED if you are satisfied :)

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • D Offline
            D Offline
            DevinQT
            wrote on last edited by
            #5

            Allright will do. Maybe it's helpful for others to list the other methods?

            aha_1980A 1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              3
              • D DevinQT

                Allright will do. Maybe it's helpful for others to list the other methods?

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by aha_1980
                #7

                @DevinQT

                I guess there are endless. e.g.:

                QString text = QString("Hello ") + QString::number(123);

                or

                QString text("Hello ");
                text.append(QString::number(123));
                

                etc.

                Qt has to stay free or it will die.

                1 Reply Last reply
                3

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved