QTimer & Thread, adding it to running thread...
-
Hey
I'm trying to add timers to a running thread so that I can have more options when I play with my worker... here is my broken idea >
auto item = new QTimer(); item->setInterval(interval); //QMetaObject::invokeMethod(item, [this, i = item]() { //item->setParent(nullptr); item->moveToThread(this); //}, Qt::QueuedConnection); QMetaObject::invokeMethod(item->thread(), [this, i = item]() { qDebug() << "Gonna run timer!"; i->start(); qDebug() << "Gonna run timer!"; }); mExtraTimers.append(item); int id = mExtraTimers.size(); qDebug() << item<<item->thread() << (QObject*)this; connect(this, &QThread::finished, item, &QObject::deleteLater, Qt::DirectConnection); connect(item, &QTimer::timeout, this, [this, i = id]() { Q_EMIT extraTriggerAction(i); }, Qt::DirectConnection); return id;
The idea is that I can add extra timers with different intervals to call a signal. Then when using direcConnection I can execute code depending on id of timer and do some processing...
But whatever I try I can't start the timer... I keep gettingQObject::startTimer: Timers can only be used with threads started with QThread
How to bite it ?TIA
-
@SGaist said in QTimer & Thread, adding it to running thread...:
With only bits of your code it's not possible to understand your implementation and the possible issues you might be facing.
Today is special day for my brain and I literally fail at basics... I forgot to start the worker... thus its event loop never run... thus it never started the damn timmer... ehhh!!!!
Everything works fine once I started the worker... yayyyy I learned something new! :- )
-
Hi,
When are calling this ?
Do you have the main event loop running ? -
@SGaist said in QTimer & Thread, adding it to running thread...:
Hi,
When are calling this ?
Do you have the main event loop running ?It's being called from the main thread after the worker started. So worker starts and it has 1 default timmer >
setObjectName(threadName + "_thread"); mTrigger = new QTimer(); mTrigger->setInterval(threadInterval); mTrigger->moveToThread(this); connect(mTrigger, &QTimer::timeout, this, &icGenericTimeoutWorker::triggerSignal,Qt::DirectConnection); connect(this, &QThread::started, this, &icGenericTimeoutWorker::startIcTimmer,Qt::DirectConnection); connect(this, &QThread::finished, mTrigger, &QObject::deleteLater,Qt::DirectConnection); connect(this, &QThread::finished, this, &QObject::deleteLater);
And the function in 1st post is where I want to add another timer to the existing worker...
-
With only bits of your code it's not possible to understand your implementation and the possible issues you might be facing.
-
@SGaist said in QTimer & Thread, adding it to running thread...:
With only bits of your code it's not possible to understand your implementation and the possible issues you might be facing.
Today is special day for my brain and I literally fail at basics... I forgot to start the worker... thus its event loop never run... thus it never started the damn timmer... ehhh!!!!
Everything works fine once I started the worker... yayyyy I learned something new! :- )