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. Leading zeroes for time with QString
Forum Updated to NodeBB v4.3 + New Features

Leading zeroes for time with QString

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 5.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.
  • B Offline
    B Offline
    BKBK
    wrote on last edited by
    #1

    The goal is to format time to display in a QLineEdit and using QString. Hours, minutes, and seconds look much better when 2 hours 3 minutes is displayed as 02:03. Here is my best shot so far:

    Time_hms = QString( “%1  %2:%3:%4” ) .arg( days,3)  
                                         .arg( hours:2 ) 
                                         .arg( minutes:2 ) 
                                         .arg( seconds );
    

    So far QString does not honor the field width. What needs to be changed?

    aha_1980A 1 Reply Last reply
    0
    • B BKBK

      The goal is to format time to display in a QLineEdit and using QString. Hours, minutes, and seconds look much better when 2 hours 3 minutes is displayed as 02:03. Here is my best shot so far:

      Time_hms = QString( “%1  %2:%3:%4” ) .arg( days,3)  
                                           .arg( hours:2 ) 
                                           .arg( minutes:2 ) 
                                           .arg( seconds );
      

      So far QString does not honor the field width. What needs to be changed?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @BKBK

      You should try this overload.

      Time_hms = QString( “%1  %2:%3:%4” ) .arg( days,3)  
                                           .arg( hours, 2, 10, QChar('0')) 
                                           .arg( minutes, 2, 10, QChar('0')) 
                                           .arg( seconds, 2, 10, QChar('0'));
      

      If your date is in a QDate, you can use the toString() function, which allows easy formatting too.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi

           auto days = 33;
            auto hours = 2;
            auto minutes = 2;
            auto seconds = 14;
            auto Time_hms = QString( "%1 %2:%3:%4" ).arg( days, 3, 10, QChar('0'))
                            .arg( hours, 2, 10, QChar('0') )
                            .arg( minutes, 2, 10, QChar('0') )
                            .arg( seconds,2,10, QChar('0') );
            qDebug() << Time_hms;
        

        output
        033 02:02:14

        Hehe. double post in exact moment :)

        B 1 Reply Last reply
        4
        • mrjjM mrjj

          Hi

             auto days = 33;
              auto hours = 2;
              auto minutes = 2;
              auto seconds = 14;
              auto Time_hms = QString( "%1 %2:%3:%4" ).arg( days, 3, 10, QChar('0'))
                              .arg( hours, 2, 10, QChar('0') )
                              .arg( minutes, 2, 10, QChar('0') )
                              .arg( seconds,2,10, QChar('0') );
              qDebug() << Time_hms;
          

          output
          033 02:02:14

          Hehe. double post in exact moment :)

          B Offline
          B Offline
          BKBK
          wrote on last edited by BKBK
          #4

          @mrjj Yeah, double post right on the money.

          We are stuck with Qt version 3 and I suspect that matters. Simplifying to a single field, this almost works

          *time_hms = QString( “x$1y” ) .arg( hours, 2, 10 );
          

          Producing: x 6y There is a space rather than the character zero.
          Add in a bit more

          *time_hms = QString( “x$1y” ) .arg( hours, 2, 10, QChar(‘0’) );
          

          To produce: x6.0000
          Actually there are about 48 zeroes after the 6. The decimal point was not present in the first shortened example.
          Edit: So I got tired of muckin around and wrote this concept four times

          if( hours < 10 ) qhours = QString( “0%1:” ) .arg( hours );
          else                         qhours =  QString( “%1:” ) .arg( hours );
          

          Then combined the strings to make the final. That’s ugly but now I get to move on.
          If there is a better solution I will use it.
          Thanks the taking the time to reply.

          J.HilkJ 1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            So in Qt3 (wow!) the
            ..2, 10, QChar(‘0’) ); part doesn't really do the same?
            What types are hours, minutes etc ?

            1 Reply Last reply
            0
            • B BKBK

              @mrjj Yeah, double post right on the money.

              We are stuck with Qt version 3 and I suspect that matters. Simplifying to a single field, this almost works

              *time_hms = QString( “x$1y” ) .arg( hours, 2, 10 );
              

              Producing: x 6y There is a space rather than the character zero.
              Add in a bit more

              *time_hms = QString( “x$1y” ) .arg( hours, 2, 10, QChar(‘0’) );
              

              To produce: x6.0000
              Actually there are about 48 zeroes after the 6. The decimal point was not present in the first shortened example.
              Edit: So I got tired of muckin around and wrote this concept four times

              if( hours < 10 ) qhours = QString( “0%1:” ) .arg( hours );
              else                         qhours =  QString( “%1:” ) .arg( hours );
              

              Then combined the strings to make the final. That’s ugly but now I get to move on.
              If there is a better solution I will use it.
              Thanks the taking the time to reply.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @BKBK
              Qt 3 should have QDateTime right ?

              does this work:

                  int year(2018),months(1),days(29),hours(8),minutes(22),seconds(0);
              
                  QDateTime dateTime(QDate(year,months,days), QTime(hours,minutes,seconds));
              
                  QString dateTimeString = dateTime.toString(QString("ddd hh:mm:ss"));
                  qDebug () << dateTimeString;
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              B 1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                @BKBK
                Qt 3 should have QDateTime right ?

                does this work:

                    int year(2018),months(1),days(29),hours(8),minutes(22),seconds(0);
                
                    QDateTime dateTime(QDate(year,months,days), QTime(hours,minutes,seconds));
                
                    QString dateTimeString = dateTime.toString(QString("ddd hh:mm:ss"));
                    qDebug () << dateTimeString;
                
                B Offline
                B Offline
                BKBK
                wrote on last edited by
                #7

                @J.Hilk
                In the example code I provided hours, minutes, etc, are all integers. Time in nanoseconds is fetched and the values extracted.
                This version does not have QtDateTime but does have QTime. I noticed that the underlying format is not explicitly stated.
                This is a strip chart function and most, probably all, the parameters to be charted arrive with a time stamp as a double. That time may be in millisecond, micros, or nanos so I wrote the basic code to work with any. Simple switch statement. Formatting was easy once each part was handled separately. Just a bit ugly 'cause there are four sets of if statements. Then just concat them:
                qtime = qdays + qhours + qmin + qseconds
                I am going to leave that code in place for now. This time I'll remember to mark the thread as resolved.
                Thanks again for your time.

                1 Reply Last reply
                0

                • Login

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