Unsolved about the longest elapsed time in Qt
-
@jsulm is the code above right? is it the best and efficient way?
-
@opengpu said in about the longest elapsed time in Qt:
ok, that's INT_MAX, int32
This is wrong as well: max(int) = 2,147,483,647. Devide it by 1000 and you get 2,147,483sec
"is the code above right? is it the best and efficient way?" - looks fine
-
Why don't you use QTimer? Seems like the optimal tool for your case (run code every xxx seconds).
Elapsed timer is for measurement.
-
@aha_1980 i neet: donot emit signal within the time-interval
-
@opengpu Then emit it AFTER the time interval - for that you can use QTimer.
-
void SendEmail() { if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.restart(); } }
SendEmail() is called at some situation, it's not called every time-interval. i just have to control inside the time-interval from last SendEmail another SendEmail is forbidden.
ie. SendEmail's minimun time interval, the time between two SendEmails >= this interval -
@opengpu
? -
@opengpu said in about the longest elapsed time in Qt:
m_ElapsedTimer.restart();
Attention:
QElapsedTimer::restart()
only works ifQElapsedTimer
instance is valid ==>QElapsedTimer::start()
has been called before!
If you don't use the return value (which is the elapsed timer since last start or restart), it is better to usestart()
:void MyClass::SendEmail() { if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.start(); } }
-
@KroMignon thanks so much!, so start when expired is ok?
because i donot when the 1st time call SendMail, so i use isValid to let me know it's the 1st time SendEmail is called.
after that it's actually check by the hasExpired.
right? -
@opengpu said in about the longest elapsed time in Qt:
so start when expired is ok?
Yes, of course.
QElapsedTimer::hasEpired()
is only a kind of helper function,
==>m_ElapsedTimer.hasExpired(timeout)
is almost the same asm_ElapsedTimer.elapsed() > timeout
. This does not affectQElapsedTimer
state.The difference is only that hasExpired() will test if timer has been started (is valid).
-
@KroMignon thank you, and i will write as follow:
QElapsedTimer m_ElapsedTimer; ... void MyClass::SendEmail() { if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.start(); } }