get milliseconds from QTimeEdit
-
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
-
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. -
@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?
-
@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?
-
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 -
@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
-
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. -