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. get milliseconds from QTimeEdit
Forum Updated to NodeBB v4.3 + New Features

get milliseconds from QTimeEdit

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.4k Views
  • 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.
  • E Offline
    E Offline
    Erni42 0
    wrote on last edited by
    #1

    I use a QTimeEdit with milliseconds.
    hitting the enter key moves the focus to the next widget.
    the time format is like "hh:MM:ss.zzz"
    If I enter three digit (like 13:12:12.555), I get the correct value in the time() value for milliseconds back. If I enter just one digit in and hit ENTER, I get no milliseconds back (like in 13:14:44.4).
    Is it a bug or a missunderstanding?

    Dietmar

    C 1 Reply Last reply
    0
    • E Offline
      E Offline
      Erni42 0
      wrote on last edited by
      #7

      Hi Chris,
      maybe I'm completely wrong. I think my problem is a different one. I grab the time value in a keyPressEvent loop. SO you enter the last number and press then Enter or Return. But the QTimeEdit didn't recognize the last entered number yet.

      if(event->key() == Qt::Key_Return|| event->key() == Qt::Key_Enter||event->key() == Qt::Key_Tab) {
          ...
      }
      

      If I put in

      if(event->key() == Qt::Key_Return|| event->key() == Qt::Key_Enter||event->key() == Qt::Key_Tab) {
              QTimeEdit::keyPressEvent(event);
      ...
      }
      

      I get the correct results.
      Thanks for your support. Without, I couldn't figure out my mistake.

      1 Reply Last reply
      0
      • E Erni42 0

        I use a QTimeEdit with milliseconds.
        hitting the enter key moves the focus to the next widget.
        the time format is like "hh:MM:ss.zzz"
        If I enter three digit (like 13:12:12.555), I get the correct value in the time() value for milliseconds back. If I enter just one digit in and hit ENTER, I get no milliseconds back (like in 13:14:44.4).
        Is it a bug or a missunderstanding?

        Dietmar

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #2

        @Erni42-0 There is a difference between the value, e.g. 44.400 seconds, and the string representation of the value which can vary, e.g "44.4", "44.40", "44.4000000", or "4.44e1" are all representations of the same value.

        How are you outputting the value "13:14:44.4"? Is it coming from one of the QTime::toString() variants, QDebug, or something else? Most default formats suppress trailing zeros to the right of a decimal point.

        Alternatively, are you talking about the value displayed in the time edit after hitting enter?

        E 1 Reply Last reply
        1
        • C ChrisW67

          @Erni42-0 There is a difference between the value, e.g. 44.400 seconds, and the string representation of the value which can vary, e.g "44.4", "44.40", "44.4000000", or "4.44e1" are all representations of the same value.

          How are you outputting the value "13:14:44.4"? Is it coming from one of the QTime::toString() variants, QDebug, or something else? Most default formats suppress trailing zeros to the right of a decimal point.

          Alternatively, are you talking about the value displayed in the time edit after hitting enter?

          E Offline
          E Offline
          Erni42 0
          wrote on last edited by
          #3

          @ChrisW67
          I take the value via time(), so the string representation is not relevant for me. The value is stored in a separate variable and then I check the value via qDebug() << variable.time()

          C 1 Reply Last reply
          0
          • E Erni42 0

            @ChrisW67
            I take the value via time(), so the string representation is not relevant for me. The value is stored in a separate variable and then I check the value via qDebug() << variable.time()

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4

            @Erni42-0 The string representation is relevant to you: that is what QDebug outputs. The QDebug helper uses QTime::toString(const QString &format) with the format "HH:mm:ss.zzz". The "zzz" component should not suppress trailing zeros and give three decimal places. Under Qt 6.6.0 and 5.15.2 (Windows MingW) that is, indeed what I see.

            #include <QApplication>
            #include <QTime>
            #include <QTimeEdit>
            #include <QDebug>
            
            int main(int argc, char *argv[]) {
                QApplication a(argc, argv);
            
                QTime time1 = QTime::fromString("13:12:12.555");
                qDebug() << time1 << time1.toString("HH:mm:ss.zzz");
                QTime time2 = QTime::fromString("13:12:12.4");
                qDebug() << time2 << time2.toString("HH:mm:ss.zzz");
                QTimeEdit edit;
                edit.setTime(time2);
                QTime time3 = edit.time();
                qDebug() << time3 << time3.toString("HH:mm:ss.zzz");
                return 0;
            }
            
            QTime("13:12:12.555") "13:12:12.555"
            QTime("13:12:12.400") "13:12:12.400"
            QTime("13:12:12.400") "13:12:12.400"
            

            So what is your platform, Qt version, and an equivalent test program that fails?

            E 1 Reply Last reply
            3
            • C ChrisW67

              @Erni42-0 The string representation is relevant to you: that is what QDebug outputs. The QDebug helper uses QTime::toString(const QString &format) with the format "HH:mm:ss.zzz". The "zzz" component should not suppress trailing zeros and give three decimal places. Under Qt 6.6.0 and 5.15.2 (Windows MingW) that is, indeed what I see.

              #include <QApplication>
              #include <QTime>
              #include <QTimeEdit>
              #include <QDebug>
              
              int main(int argc, char *argv[]) {
                  QApplication a(argc, argv);
              
                  QTime time1 = QTime::fromString("13:12:12.555");
                  qDebug() << time1 << time1.toString("HH:mm:ss.zzz");
                  QTime time2 = QTime::fromString("13:12:12.4");
                  qDebug() << time2 << time2.toString("HH:mm:ss.zzz");
                  QTimeEdit edit;
                  edit.setTime(time2);
                  QTime time3 = edit.time();
                  qDebug() << time3 << time3.toString("HH:mm:ss.zzz");
                  return 0;
              }
              
              QTime("13:12:12.555") "13:12:12.555"
              QTime("13:12:12.400") "13:12:12.400"
              QTime("13:12:12.400") "13:12:12.400"
              

              So what is your platform, Qt version, and an equivalent test program that fails?

              E Offline
              E Offline
              Erni42 0
              wrote on last edited by Erni42 0
              #5

              @ChrisW67

              For the input "14:20:25.1" instead of "14:20:25.100"

              void my_TimeEdit::keyPressEvent(QKeyEvent *event)
              {
                  if(event->key() == Qt::Key_Return|| event->key() == Qt::Key_Enter||event->key() == Qt::Key_Tab) {
                              qDebug() << this->text();
                              int milliSeconds = this->time().msec();
                              qDebug() << milliSeconds;
                  }
              }
              

              gives
              "14:20:25.1"
              0
              as output on Debian Sid with Qt 5.15 as on Qt 6.6 on Windows

              C 1 Reply Last reply
              0
              • E Erni42 0

                @ChrisW67

                For the input "14:20:25.1" instead of "14:20:25.100"

                void my_TimeEdit::keyPressEvent(QKeyEvent *event)
                {
                    if(event->key() == Qt::Key_Return|| event->key() == Qt::Key_Enter||event->key() == Qt::Key_Tab) {
                                qDebug() << this->text();
                                int milliSeconds = this->time().msec();
                                qDebug() << milliSeconds;
                    }
                }
                

                gives
                "14:20:25.1"
                0
                as output on Debian Sid with Qt 5.15 as on Qt 6.6 on Windows

                C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #6

                @Erni42-0 You are not doing what I demonstrated.
                qDebug() << this->text(); outputs "14:20:25.1".
                You are calling QAbstractSpinBox::text() which returns, "the spin box's text, including any prefix and suffix." That string could be in a multitude of forms, and QDebug just displays the string it is given. Adapting my example:

                QTimeEdit edit;
                edit.setTime(time2);
                QTime time3 = edit.time();
                qDebug() << time3 << time3.toString("HH:mm:ss.zzz");
                
                qDebug() << edit.displayFormat() << edit.text();
                edit.setDisplayFormat("hh:mm:ss.zzz");
                qDebug() << edit.displayFormat() << edit.text();
                
                QTime("13:12:12.400") "13:12:12.400"
                "h:mm AP" "1:12 PM"
                "hh:mm:ss.zzz" "13:12:12.400"
                

                My example provided QDebug with a QTime object, which outputs in a very specific format.

                As for the assertion that, with the time "14:20:25.1", this returns 0:

                int milliSeconds = this->time().msec();
                qDebug() << milliSeconds;
                

                I can only conclude you are mistaken.

                Expanding on my example:

                 qDebug() << edit.time() << edit.time().msec();
                int msec = edit.time().msec();
                qDebug() << msec;
                
                QTime("13:12:12.400") 400
                400
                

                or, with your time value:

                QTime("14:20:25.100") 100
                100
                
                1 Reply Last reply
                3
                • E Offline
                  E Offline
                  Erni42 0
                  wrote on last edited by
                  #7

                  Hi Chris,
                  maybe I'm completely wrong. I think my problem is a different one. I grab the time value in a keyPressEvent loop. SO you enter the last number and press then Enter or Return. But the QTimeEdit didn't recognize the last entered number yet.

                  if(event->key() == Qt::Key_Return|| event->key() == Qt::Key_Enter||event->key() == Qt::Key_Tab) {
                      ...
                  }
                  

                  If I put in

                  if(event->key() == Qt::Key_Return|| event->key() == Qt::Key_Enter||event->key() == Qt::Key_Tab) {
                          QTimeEdit::keyPressEvent(event);
                  ...
                  }
                  

                  I get the correct results.
                  Thanks for your support. Without, I couldn't figure out my mistake.

                  1 Reply Last reply
                  0
                  • E Erni42 0 has marked this topic as solved on

                  • Login

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