[Interest] Difficulty running Timer from QThread

Björn Schäpers qt-maillist at hazardy.de
Mon Jul 19 08:08:13 CEST 2021


Hi,

you should parent your QTimer, and moveToThread afterwards. Then the Timer will 
also be moved to the thread, otherwise it remains whereever you created the worker.

Regards,
Björn.

Am 19.07.2021 um 05:29 schrieb Israel Brewster:
> Yes, this is a FAQ, but I can’t seem to figure out what I am doing wrong. I am 
> using the worker pattern of QThread, (as opposed to subclassing), running Qt 
> 5.15.2. In my controller’s constructor, I have the following code to 
> initialize a worker:
>
> worker=newVLCWorker();
> worker->moveToThread(&vlcWorkerThread);
> worker->initVLC(videoWin);
> vlcWorkerThread.start();
>
> The initVLC function, among other things, creates a timer and connects it to a 
> slot in the worker:
>
> rtspStartTimer=newQTimer;
> rtspStartTimer->setSingleShot(true);
> connect(rtspStartTimer,&QTimer::timeout,
> this,&VLCWorker::rtspStarting);
>
> Later, at some point during program execution, I try to start this timer from 
> a function in the worker (which itself was triggered by a signal from the main 
> thread):
>
> qDebug()<<"*****Tryingtostarttimerwhichlivesinthread"<<(long)rtspStartTimer->thread()<<"fromthread"<<(long)this->thread();
> rtspStartTimer->start(250);
> qDebug()<<"*****Timerattemptcomplete";
>
> Unfortunately, this doesn’t work, giving me the following output:
>
> Debug: 2021-07-18T19:26:12.009 - *****Trying to start timer which lives in 
> thread 140235980165688  from thread  140235980165688 
> (../DoorBellCamC/vlccontroller.cpp:121, void VLCWorker::rtspStarting())
> Warning: 2021-07-18T19:26:12.009 - QObject::startTimer: Timers cannot be 
> started from another thread (:0, )
> Debug: 2021-07-18T19:26:12.009 - *****Timer attempt complete 
> (../DoorBellCamC/vlccontroller.cpp:123, void VLCWorker::rtspStarting())
>
> So even though the output of the debug confirms that the timer lives in the 
> same thread I am trying to call start from, I still get the error about 
> “timers cannot be started from another thread”. What am I doing wrong?
> ---
> Israel Brewster
> Software Engineer
> Alaska Volcano Observatory
> Geophysical Institute - UAF
> 2156 Koyukuk Drive
> Fairbanks AK 99775-7320
> Work: 907-474-5172
> cell:  907-328-9145
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210719/4139aeac/attachment.html>


More information about the Interest mailing list