PDA

View Full Version : QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait()



airproject
4th August 2012, 12:56
Hi guys!
I have the following problem:
1. I want get thread doing some work and update the label in the main thread every second.
2. I got it working subclassing QThread bu according to some sources (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/) it is not a good way.
3. I decided to subclass a QObject (MyObject) with internal QTimer* _myTimer, and doWork() function with an internal loop, This object is after construction moved to newly created QThread* _thread.

My problem is that _myTimer's timeout() signal which is connected to tick() slot of the _myObject, is not executed after _thread->start(). tick() is executed only after doing _thread->wait(). Both the tick() slot and myTimer exist in the myThread so i do not know what is the problem. It seems like there is no event loop in myThread.

Here is my code:


class MyObject : QObject
{
Q_OBJECT
public:
MyObject(QObject *parent=0);

public slots:
void tick();
void doWork();

private:
QTimer* _myTimer;
};

void MyObject::doWork()
{
_myTimer = new QTimer(this);
_myTimer->setInterval(1000);
connect(_myTimer, SIGNAL(timeout()), this, SLOT(tick())/*,Qt::DirectConnection*/);
_myTimer->start()
qDebug() << QThread::currentThreadId() << "doWork()";
while(..)
{
...
}
}

void MyObject::tick()
{
emit tickSignalToGUI(..);
qDebug() << QThread::currentThreadId() << "tick()";
}

// and somewhere in GUI thread
(...)
_thread = new QThread(this);
_myObject = new MyObject(); /
_myObject->moveToThread(_thread);
_thread->start();
// _myObject->doWork(); //this blocks execution
QMetaObject::invokeMethod(_myObject,"doWork");
qDebug() << QThread::currentThreadId() << "GUI thread";
(...)


Any ideas what is wrong with this approach?

best regards,

amleto
4th August 2012, 13:31
what you are doing is even worse than subclassing QThread (not that subclassing QThread is bad)


You are blocking the main thread with the while(...) loop in MyObject.

airproject
4th August 2012, 13:37
what you are doing is even worse than subclassing QThread (not that subclassing QThread is bad)


You are blocking the main thread with the while(...) loop in MyObject.

Are you sure? As myObject with its while loop is moved to another thread it seems to run concurently to main thread. At least the GUI stays responsive and processes all the events.

best regads.

amleto
4th August 2012, 14:15
well, now that you have changed it to QMetaObject::invokeMethod with your edit it should be a lot better... :rolleyes:

Now you're just blocking the other thread with that while(...)


You know it would be handy if you didn't hide code that is directly relevant to your question. :rolleyes:

airproject
4th August 2012, 14:20
sorry for that.. your footnote really make sense;)
Do you have any idea why the signals from timer are not processed during the doWork()? They start to be processed after i try to finish _myThread with _mythread->wait(). :/

I cant find any good way to let the timer work in a separate thread.:(

amleto
4th August 2012, 14:23
how many times do you think I shall say that your while(...) is* blocking before you will think that might be the cause?

*guessing since you are still hiding that code...

airproject
4th August 2012, 14:30
One with some additional comment would be enough.
So how to avoid this devilish while(..) and still do some processing loop that can coexist with the timer in harmony?

amleto
4th August 2012, 14:36
use signals and slots. not while(true) or equivalent

airproject
4th August 2012, 14:54
My doWork() is processing some number of items. It should process this data unless user presses Pause button or Stop button. I initially made a QThread subclass which in run() had while(true) (i guess that this is not a bad practice in thread?) and some exit conditions checking flags set by signals from GUI. As "this->moveToThread(this)" trick is not a good thing to do i have rebuild my QThread subclass to QObject subclass.
How would you rebuild the while(true){} processing loop to fit "QObject moved to QThread" approach?
Best regards.

amleto
4th August 2012, 15:39
How do you expect me to say how you can refactor your while loop when you have given no information about what it actually does and where the input comes from :mad::mad::confused::rolleyes:

I give up. Just use this.
qcoreapplication.html#processEvents

airproject
4th August 2012, 15:45
If you suggest to use:


doWork()
{
(...)
while(true)
{
QCoreApplication::processEvents();
(...)

}

it does not solve the problem.

amleto
4th August 2012, 15:46
read my sig


All your timer does is send signal. you can put your timer anywhere. Why does it have to be in heavy processing thread?

airproject
4th August 2012, 16:22
I can redesign and put a timer outside the processing thread. Still for my own knowledge i would like to know it there is a way of installing the QTimer inside a Thread.

Sorry for not pasting the original code. It is not my property and i cannot share it. As it is quite big i just tried to extract the highlights.

amleto
4th August 2012, 18:19
You can put a timer anywhere you want, including in threads. But if you block the event loop of that thread then signals wont fly out and slots inside wont be executed. It's pretty straight forward.

airproject
4th August 2012, 18:49
I agree with you. I do not know how to make these timer events propagate in my while() loop. According to docs: QCoreApplication::processEvents()
"Processes all pending events for the calling thread according to the specified flags until there are no more events to process."

So putting it inside a while() loop should do the job - sadly it does not, ..or i am doing something wrong.
Thank you anyway.

amleto
4th August 2012, 19:11
you could be doing any number of wrong things so I'm not going to speculate any further without code.