Implementing a "Time Left" Function
-
Hello everyone.
So, I was able to get a system process delay function working, which gets passed an integer and delays system processes for that many milliseconds that the integer asked for.
I'm trying to work this code into being able to display the time left, in seconds, into a label. So far, nothing has been working.Any help would be necessary.
CODEvoid MainGame2::gameDelay(int msecs) { ///variables int timeLeft = msecs; //used in tandem with progress bar QTime endTime= QTime::currentTime().addMSecs(msecs); while( QTime::currentTime() < endTime ) { ///QCoreApplication handles event loops without UIs QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } }//end function delay
-
@Thugzook
It is not completely clear what problem are you see. However, I think you should have a look at the msecsTo funtionality. This is also available for QDateTime and may be easier for bridging days. Also currentDateTime may be of use for you. -
I'm not quite sure how to convert to seconds, it what I'm saying.
I'm trying to use a while loop to continue to processEvents until the variable endTime.Not quite sure how to work this out nor how to effectively work an integer countDown within that loop.
-
I would suggest taking a look at QElapsedTimer. This class allows you to start a "count up" timer.
One of the methods of the class is elapsed() which returns the number of milliseconds since the timer was started.
By knowing how long you want to allow (total time perhaps) and using an elapsed timer with elapsed() the difference between the two will be the remaining time, in milliseconds.
To convert it to a nice human readable form there are lots of ways but you could simply code up (not the prettiest code, downloaded from a stack overflow post):
QString ConvertMStoHumanTime( qint64 ms, bool showDays, bool showMS ) { QString Result; double interval; qint64 intval; // Days interval = 24.0 * 60.0 * 60.0 * 1000.0; intval = (qint64)trunc((double)ms / interval); if( intval<0 ) intval = 0; ms -= (qint64)trunc(intval * interval); qint32 days = intval; // Hours interval = 60.0 * 60.0 * 1000.0; intval = (qint64)trunc((double)ms / interval); if( intval<0 ) intval = 0; ms -= (qint64)trunc(intval * interval); qint32 hours = intval; // Minutes interval = 60.0 * 1000.0; intval = (qint64)trunc((double)ms / interval); if( intval<0 ) intval = 0; ms -= (qint64)trunc(intval * interval); qint32 minutes = intval; // Seconds interval = 1000.0; intval = (qint64)trunc((double)ms / interval); if( intval<0 ) intval = 0; ms -= (qint64)trunc(intval * interval); qint32 seconds = intval; // Whatever is left over is milliseconds char buffer[25]; memset( buffer, 0, 25 ); if( showDays ) { if( days<10 ) sprintf_s( buffer, "%d", days ); Result.append( QString("%1d ").arg(buffer) ); } if( hours<10 ) sprintf_s( buffer, "0%d", hours ); else sprintf_s( buffer, "%d", hours ); Result.append( QString("%1:").arg(buffer) ); if( minutes<10 ) sprintf_s( buffer, "0%d", minutes ); else sprintf_s( buffer, "%d", minutes ); Result.append( QString("%1:").arg(buffer) ); if( seconds<10 ) sprintf_s( buffer, "0%d", seconds ); else sprintf_s( buffer, "%d", seconds ); Result.append( QString("%1").arg(buffer) ); if( showMS ) { if( ms<10 ) sprintf_s( buffer, "00%d", ms ); else if( ms<100 ) sprintf_s( buffer, "0%d", ms ); else sprintf_s( buffer, "%d", ms ); Result.append( QString(".%1").arg(buffer) ); } return Result; }