timer - How to pause and resume a Qtimer (Qt 5) -


i need on usage of qtimer.

i work qt 5.0.2 , here problem :

i trying develop timer, , interface simple :

there 2 button : button "start", launch timer, , "pause" button, , qtimeedit display time.

this screenshot shows how looks : http://img834.imageshack.us/img834/1046/5ks6.png

the problem pause function doesn't work. have read documentation qtimer here : http://harmattan-dev.nokia.com/docs/library/html/qt4/qtimer.html , here : qt.developpez.com/doc/5.0-snapshot/qtimer/ , no result.

this source code have : (i put needed)

// creation of buttons , time area void mainwindow::createbottom() {      bottom = new qwidget();      play = new qpushbutton("launch",this);     pause = new qpushbutton("pause",this);     play->setdisabled(false);     pause->setdisabled(true);     timeedit = new qtimeedit(this);     timeedit->setdisplayformat("mm:ss");      layout->addwidget(play);     layout->addwidget(pause);     layout->addwidget(timeedit );     bottom->setlayout(layout);      connect(play, signal(clicked()), this, slot(startsimulation()));     connect(pause, signal(clicked()), this, slot(pausesimulation())); }  // resume timer stopped void mainwindow::resumesimulation() {     timer->blocksignals( false );     pause->settext("pause");     pause->disconnect(signal(clicked()));     connect(pause, signal(clicked()), this, slot(pausesimulation()));     paused = false;      timer->start();     int timeofpause = time->restart();     int timetotal = timeofpause + timeelapsed;     time->addmsecs(-timetotal);  }  // start timer void mainwindow::pausesimulation() {     timer->blocksignals(true);     pause->settext("resume");     timer->stop();     play->setdisabled(false);     //pause->setdisabled(true);     pause->disconnect(signal(clicked()));      connect(pause, signal(clicked()), this, slot(resumesimulation()));      paused = true; }  // start timer zero. void mainwindow::startsimulation() {      timer = new qtimer(this);     connect(timer, signal(timeout()), , slot(updatetime()));     timer->start(500);     play->setdisabled(true);      pause->setdisabled(false); }  void mainwindow::updatetime() {     if(time == null)     {        time = new qtime(0,0,0,0);        time->start();     }     //timeedit->settime(qtime::froms(time->elapsed()));     //time = &(time->addmsecs(1000));     if(hasrestart)     {         time->restart();         time->addmsecs(-timeelapsed);         hasrestart = false;     }     else     {         timeelapsed =+ time->elapsed();     }     int seconds = 0;     int minutes = 0;     int hours = 0;      if(!paused)     {             seconds = (timeelapsed/1000)%60;             minutes = (timeelapsed/60000)%60;             hours =  (timeelapsed/3600000)%24;             std::cout << "test : " << hours << ":" << minutes << ":" << seconds << std::endl;             timeedit->settime(qtime(0,minutes,seconds,0));             timeedit->update();     } } 

when push start button, timer starts well, when push "pause" pause on graphic interface, when resume, shows present time if hadn't paused.

for instance :

i start. pause @ 00:05. blocks apparently timer. wait 10 seconds. resume timer, shows 00:15 instead of 00:06

how fix ?

thank !

edit : kuba ober, explain me code posted please ?

how pause work ?

below sscce, tested under both qt 4.8 , 5.1.

screenshot of example

//main.cpp #include <qapplication> #include <qpushbutton> #include <qvboxlayout> #include <qlabel> #include <qelapsedtimer> #include <qtime>  class window : public qwidget {     q_object     int m_timerid;     qint64 m_accumulator;     qlabel *m_label;     qelapsedtimer m_timer;     q_slot void on_restart_clicked() {         m_accumulator = 0;         m_timer.restart();         if (m_timerid == -1) m_timerid = starttimer(50);     }     q_slot void on_pause_clicked() {         if (m_timer.isvalid()) {             m_accumulator += m_timer.elapsed();             m_timer.invalidate();         } else {             m_timer.restart();             m_timerid = starttimer(50);         }     }     void timerevent(qtimerevent * ev) {         if (ev->timerid() != m_timerid) {             qwidget::timerevent(ev);             return;         }         qtime t(0,0);         t = t.addmsecs(m_accumulator);         if (m_timer.isvalid()) {             t = t.addmsecs(m_timer.elapsed());         } else {             killtimer(m_timerid);             m_timerid = -1;         }         m_label->settext(t.tostring("h:m:ss.zzz"));     } public:     explicit window(qwidget *parent = 0, qt::windowflags f = 0) : qwidget(parent, f), m_timerid(-1)  {         qvboxlayout * l = new qvboxlayout(this);         qpushbutton * restart = new qpushbutton("start");         qpushbutton * pause = new qpushbutton("pause/resume");         restart->setobjectname("restart");         pause->setobjectname("pause");         m_label = new qlabel("--");         l->addwidget(restart);         l->addwidget(pause);         l->addwidget(m_label);         qmetaobject::connectslotsbyname(this);     } };  int main(int argc, char *argv[]) {     qapplication a(argc, argv);     window w;     w.show();     return a.exec(); }  #include "main.moc" 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -