PDA

View Full Version : display number on label



aj2903
12th March 2009, 07:06
hii..
I want to display number on the label;
I can do this by following code :



int num;
QString qstr = QString::number(num);
ui.label->setText(qstr);


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?

spirit
12th March 2009, 07:13
I think like this


int num;
QString qstr = QString::number(num);
if (num <= 9 && num >= 0)
qstr.insert(0, "0");
else if (num >= -9 && num <= 0)
qstr.insert(1, "0");
ui.label->setText(qstr);

:)

radek.z
12th March 2009, 07:39
maybe you can aslo try QRegExp to check if it has only one digit and if yes then prepend 0

Lesiok
12th March 2009, 08:10
int num;
ui.label->seText(QString("%1").arg(num,2,'0'));

spirit
12th March 2009, 08:24
int num;
ui.label->seText(QString("%1").arg(num,2,'0'));

the first
you exmaple works after modification under Qt 4.5.0


ui.label->seText(QString("%1").arg(num, 2, 10, QChar('0'));

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"