April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

Formatting Numbers

 

Hi guys,

I’m new on developing software on Qt. I would like to get some numbers formatted as string with 2 digits for every numbers. This to get the numbers with value < 9 formatted as “09”.
I’m using the lines of code below but they don’t give me back what I would like to obtain and I can’t understand where the mistake is. Please could you help me and check my code?

  1.        
  2.        uint hours=1, minutes=2, seconds=3;
  3.        QString str = QString("%1:%2:%3")
  4.                 .arg(hours, 1, 10)
  5.                 .arg(minutes, 2, 10, '0')
  6.                 .arg(seconds, 2, 10, '0');
  7.         ui->chronoDisplay->setText(str);  //chronoDisplay is a QLabel
  8.         //I'm expecting that str has got this format: "1:02:03"

Thank you in advance.

14 replies

April 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

I guess that this is getting in your way:


If fillChar is ‘0’ (the number 0, ASCII 48), the locale’s zero is used.

(from the QString documentation)

I am wondering, why you are not using QTime to format your time:

  1. QTime t(hours, minutes, seconds);
  2. ui->chronoDisplay->setText(t.toString("h:mm::ss"));

Edit: fixed second line of above code (replaced QTime:: with t. )

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts
Andre wrote:
I guess that this is getting in your way:
If fillChar is ‘0’ (the number 0, ASCII 48), the locale’s zero is used.
(from the QString documentation) I am wondering, why you are not using QTime to format your time:
  1.  QTime t(hours, minutes, seconds); ui->chronoDisplay->setText(QTime::toString("h:mm::ss"));

Just because I didn’t know this possibility… Now that I know it I’m going to try to use it!

Thank you for the advice!

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

Hi Andre,

I’ve followed your advice but I’ve got the same result.

I had forgotten to say that I want this application to run under symbian 3 OS. Could be this the problems?

April 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

Could be. Could you check the ouput of QLocale::systemLocale().zeroDigit() ?

If that is a space, then I guess you have your culprit…

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

I don’t think so because either the techniques I’ve followed, have given me back a string filled with “0”, the problem is that the number of 0 returned are more than expected!

Despite the fact I think the problem is different, how can I set the zeroDigit() property to be sure that it returns a ‘0’?

April 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

You can’t set that value from inside Qt. It is set by the system. However, could you show us what your output is now then? I was under the impression from your previous posts that you did not get enough 0’s. Now, you tell us, you get too many. So how does your output look?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

I would like to show you an image which shows the output I’m receiving on the device simulator, but how can I link this image?

April 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

Just put the image on some public place (I use my public DropBox folder), and you can link it into your message using the small picture icon in the bar above the editor window.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

Ok, this is the result I’m gettim from the simulator after the code is processed.

nokia simulator image

this is the URL of the image:
http://www.softwareprojects.it/images/blogimages/software/nokiaSimulator.png

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts
Andre wrote:
  1.  QTime t(hours, minutes, seconds); ui->chronoDisplay->setText(QTime::toString("h:mm::ss"));

Why this code worked just one time and now it doesn’t work again?

This is the error I’m receiving when I try to use the code above

C:\Documents and Settings\tlp31\Documenti\C++_Project\chrono-build-simulator\..\chrono\mainwindow.cpp:120: error: cannot call member function ‘QString QTime::toString(const QString&) const’ without object

and this is the code:

  1.  QTime t(hours, minutes, seconds);
  2. ui->chronoDisplay->setText(QTime::toString("h:mm::ss"));

April 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

You need to call t::toString, not QTime::toString in your second line. The error message describes that quite clearly, I think.

Note: I now see that I wrote it wrong in my example. My apologies.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

I’m sorry but it still doesn’t work. The compiler says: [..] 121: error: ‘t’ is not a class or namespace

How I have to initialize a QTime class properly?

April 3, 2012

Andre Andre
Area 51 Engineer
6031 posts

  1.      QTime time(hours, minutes, seconds);
  2.      ui->chronoDisplay->setText(time.toString("h:mm::ss"));

This should work.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 3, 2012

gRicky gRicky
Ant Farmer
57 posts

Yes it works, and with this modification also the UI is working properly.

Thank you Andre for your support in this matter.

 
  ‹‹ qsharedmemory problem      Re-engineer the Simple Tree Model ››

You must log in to post a reply. Not a member yet? Register here!