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 put a string variable as the text of the qlabel.
Forum Updated to NodeBB v4.3 + New Features

How to put a string variable as the text of the qlabel.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 15.0k 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.
  • H Offline
    H Offline
    hoomz
    wrote on 7 Nov 2014, 23:36 last edited by Chris Kawa
    #1

    I'm trying to do this:

    QLabel* label=new QLabel("Welcome ",temp->getLoggedInID());
    

    where temp->getLoggedInID() returns a string. So i want the text of the qlabel to be something like: "Welcome Mike"

    But this doesn't work. I would I do this?

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Nov 2014, 23:41 last edited by Chris Kawa
      #2

      Hi and welcome to devnet,

      You can't build a string like that.

      QString("Welcome %1").arg(temp->getLoggedInID())
      

      Should do the job

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hoomz
        wrote on 8 Nov 2014, 00:24 last edited by Chris Kawa
        #3

        Hi SGaist thanks for your help, but I didn't quite understand your solution.

        Let's say I converted two strings to qstring:

        qstring1
        qstring2

        How would i put them into the qlabel:

        QLabel* label=new QLabel("Welcome "......)
        

        Thanks!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gyll
          wrote on 8 Nov 2014, 00:49 last edited by
          #4

          how about QString("Welcome ")+whateverElseQString :P

          1 Reply Last reply
          0
          • C Online
            C Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on 8 Nov 2014, 10:20 last edited by Chris Kawa
            #5

            @gyll - The .arg() option that SGaist gave is better because of translation system. In some languages the name might go before the "welcome" in which case the translator can just change the template text for that language to "%1 welcome" without re-compiling the app. Of course this would need to be wrapped in tr() first but you get the point.

            @hoomz

            QString str1 = "foo";
            QString str2 = "bar"; 
            QString text = QString("I like %1 more than %2.").arg(str1, str2);
            
            QLabel* label = new QLabel(text); //if it's a new label
            label->setText(text); //if it's an existing one
            
            1 Reply Last reply
            2

            1/5

            7 Nov 2014, 23:36

            • Login

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