[Interest] QTimer and QThread

Dmitry Kozlov gnitry at mail.ru
Wed Jul 31 16:06:20 CEST 2013


On 31.07.2013 17:01, Alexander Syvak wrote:
> Here's the snippet below from implementation of the run method. The 
> class inherits a class inheriting QThread.
> ...
> screenshot_qtmr=newQTimer;//(this);
>      screenshot_qtmr->moveToThread(this);
>      qDebug()  <<  connect(screenshot_qtmr,  SIGNAL(timeout()),  this,  SLOT(on_screenshot_timeout()));
>      qDebug()  <<  connect(this,  SIGNAL(kill_tmr()),  screenshot_qtmr,  SLOT(stop()));
>      screenshot_qtmr->start(freq*1000);
>      anal_qmr  =  new  QTimer;//(this);
>      anal_qmr->moveToThread(this);
>      qDebug()  <<  connect(anal_qmr,  SIGNAL(timeout()),  SLOT(on_analize_timeout()));
>      qDebug()  <<  connect(this,  SIGNAL(kill_tmr()),  anal_qmr,  SLOT(stop()));
>      anal_qmr->start(state_analizer_tmr_interval);
>      if  (  !state_analizer_tmr_id  )  return;
>
>      exec();
>      while  (  !stop  );
>
> }
>
> Here's an ouput
> run() "./13_7_31_10_49_2_974"
>
> "./13_7_31_10_49_2_974 exists"
>
> "./13_7_31_10_49_2_974 has 'write' permission for other"
>
> "./13_7_31_10_49_2_974/S0 directory was created"
>
> true
>
> true
>
> true
>
> true
>
> run() "./13_7_31_10_49_2_974"
>
> "./13_7_31_10_49_2_974 exists"
>
> "./13_7_31_10_49_2_974 has 'write' permission for other"
>
> "./13_7_31_10_49_2_974/S1 directory was created"
>
> true
>
> true
>
> true
>
> true
>
>
> It never enters those method specified on timeout(). Why?
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

1) From QTimer documentation:

    */Qt uses the timer's/**/thread affinity
    <https://qt-project.org/doc/qt-4.8/qobject.html#thread>/**/to
    determine which thread will emit the/**/timeout()
    <https://qt-project.org/doc/qt-4.8/qtimer.html#timeout>/**/signal.
    Because of this, you must start and stop the timer in its thread; it
    is not possible to start a timer from another thread./*

But you starts timer from source thread. Timer will not starts.
You should use something like:

screenshot_qtmr=newQTimer;//(this);
screenshot_qtmr->setInterval(freq*1000);

screenshot_qtmr->moveToThread(this);
qDebug() << connect(this, SIGNAL(started()),screenshot_qtmr, SLOT(start()));

qDebug()  <<  connect(screenshot_qtmr,  SIGNAL(timeout()),  this,  SLOT(on_screenshot_timeout()));

qDebug()  <<  connect(this,  SIGNAL(kill_tmr()),  screenshot_qtmr,  SLOT(stop()));

//screenshot_qtmr->start(freq*1000); // do not do it



2) In you sample on_screenshot_timeout will be executed in main thread, 
maybe you need change

connect(screenshot_qtmr,  SIGNAL(timeout()),  this,  SLOT(on_screenshot_timeout()));
to
connect(screenshot_qtmr,  SIGNAL(timeout()),  this,  SLOT(on_screenshot_timeout()), Qt::DirectConnection);
?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130731/6d26a158/attachment.html>


More information about the Interest mailing list