[Interest] Difficulty running Timer from QThread

Nuno Santos nuno.santos at imaginando.pt
Mon Jul 19 06:48:29 CEST 2021


Israel,

Maybe the problem is that fact that you call worker initVLC(videoWin) from the main thread.

Maybe you could connect a signal to the thread started signal to call initVLC.

Try to debug QThread::currentThread() when initVLC is called and when worker is created to see if what I say makes sense.

Best,

Nuno

> On 19 Jul 2021, at 04:29, Israel Brewster <ijbrewster at alaska.edu> wrote:
> 
> 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=new VLCWorker();
> 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=new QTimer;
> 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()<<"*****Trying to start timer which lives in thread"<<(long)rtspStartTimer->thread()<<" from thread "<<(long)this->thread();
> rtspStartTimer->start(250);
> qDebug()<<"*****Timer attempt complete";
> 
> 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/b92735c7/attachment.html>


More information about the Interest mailing list