Results 1 to 5 of 5

Thread: display number on label

  1. #1
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default display number on label

    hii..
    I want to display number on the label;
    I can do this by following code :

    Qt Code:
    1. int num;
    2. QString qstr = QString::number(num);
    3. ui.label->setText(qstr);
    To copy to clipboard, switch view to plain text mode 

    my problem is if the number is single digit i.e 2 i want to display it as 02 in label.
    how can this be done?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: display number on label

    I think like this
    Qt Code:
    1. int num;
    2. QString qstr = QString::number(num);
    3. if (num <= 9 && num >= 0)
    4. qstr.insert(0, "0");
    5. else if (num >= -9 && num <= 0)
    6. qstr.insert(1, "0");
    7. ui.label->setText(qstr);
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    aj2903 (12th March 2009)

  4. #3
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: display number on label

    maybe you can aslo try QRegExp to check if it has only one digit and if yes then prepend 0

  5. The following user says thank you to radek.z for this useful post:

    aj2903 (12th March 2009)

  6. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display number on label

    Qt Code:
    1. int num;
    2. ui.label->seText(QString("%1").arg(num,2,'0'));
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lesiok for this useful post:

    aj2903 (12th March 2009)

  8. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: display number on label

    Quote Originally Posted by Lesiok View Post
    Qt Code:
    1. int num;
    2. ui.label->seText(QString("%1").arg(num,2,'0'));
    To copy to clipboard, switch view to plain text mode 
    the first
    you exmaple works after modification under Qt 4.5.0
    Qt Code:
    1. ui.label->seText(QString("%1").arg(num, 2, 10, QChar('0'));
    To copy to clipboard, switch view to plain text mode 
    the second it adds zero only for positive values.
    "-20"
    "-19"
    "-18"
    "-17"
    "-16"
    "-15"
    "-14"
    "-13"
    "-12"
    "-11"
    "-10"
    "-9"
    "-8"
    "-7"
    "-6"
    "-5"
    "-4"
    "-3"
    "-2"
    "-1"
    "00"
    "01"
    "02"
    "03"
    "04"
    "05"
    "06"
    "07"
    "08"
    "09"
    "10"
    "11"
    "12"
    "13"
    "14"
    "15"
    "16"
    "17"
    "18"
    "19"
    "20"
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Display row Number in QMessageBox
    By arunvv in forum Newbie
    Replies: 6
    Last Post: 2nd May 2008, 00:24
  2. how to display micro(mu) symbol in label
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 7th March 2008, 11:59
  3. Display a Label on top of a QGlWidget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2008, 17:11
  4. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 17:34
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13

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.