Results 1 to 2 of 2

Thread: Label with text and number

  1. #1
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Label with text and number

    Hey,
    I have a Label and I want to put in a number and a text.
    Qt Code:
    1. int a;
    2. ui->label->setText(a "is the answer");
    To copy to clipboard, switch view to plain text mode 

    but this does not work, because qt can not see a there as a number. Maybe someone can help me?

    Also when I want the label to say a over b I would write "a/b" and use
    Qt Code:
    1. int a;
    2. int b;
    3. ui->label->setNum(a/b);
    To copy to clipboard, switch view to plain text mode 

    But obviously it calculates the result. I think it would also be possible if I could put Numbers in the setText function.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Label with text and number

    Look at the QString::arg() methods for formatting strings with extra arguments.

    Qt Code:
    1. int a = 42;
    2. QString formattedString = QString( "%1 is the answer to life, the universe, and everything" ).arg( a );
    To copy to clipboard, switch view to plain text mode 

    Cracking open a C++ textbook would help you, too. You should realize that

    Qt Code:
    1. ui->label->setText( a "some string" );
    To copy to clipboard, switch view to plain text mode 

    isn't correct C++ syntax.
    Last edited by d_stranz; 30th June 2020 at 21:58.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Vertical label text
    By Maxxximo in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2020, 18:32
  2. Number to Text
    By Markov in forum Newbie
    Replies: 2
    Last Post: 22nd February 2017, 10:16
  3. how to append text in label
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 12th March 2009, 12:06
  4. display number on label
    By aj2903 in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 08:24
  5. label with image and text
    By mattia in forum Newbie
    Replies: 1
    Last Post: 7th March 2008, 12:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.