ctarsoaga
23rd January 2015, 09:24
Hi
I try to use a simple thread
class MyThread : public QThread
{
Q_OBJECT
private:
virtual void run()
{
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout), this, SLOT(onTimer()));
timer.start(1000);
QThread::exec();
}
private slots:
void onTimer() {....}
};
in an application that does not have a QCoreApplication event loop in the main thread, e.g.
void main()
{
QCoreApplication app;
//no app.exec() here, we use our custom loop
forever()... {}
}
I start an instance of MyThread but I never receive any timer events.
I am not able to add app.exec() in the main thread so I added a QEventLoop somewhere and then I started to receive my timer events.
But I don't understand what is going on: I expected to have the timer events working since I do have an event loop inside my thread.
I cannot check now, but it just came to me that maybe the onTimer() slot gets called on a different thread.
Can that be the problem?
Can somebody please explain?
Thanks a lot
I try to use a simple thread
class MyThread : public QThread
{
Q_OBJECT
private:
virtual void run()
{
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout), this, SLOT(onTimer()));
timer.start(1000);
QThread::exec();
}
private slots:
void onTimer() {....}
};
in an application that does not have a QCoreApplication event loop in the main thread, e.g.
void main()
{
QCoreApplication app;
//no app.exec() here, we use our custom loop
forever()... {}
}
I start an instance of MyThread but I never receive any timer events.
I am not able to add app.exec() in the main thread so I added a QEventLoop somewhere and then I started to receive my timer events.
But I don't understand what is going on: I expected to have the timer events working since I do have an event loop inside my thread.
I cannot check now, but it just came to me that maybe the onTimer() slot gets called on a different thread.
Can that be the problem?
Can somebody please explain?
Thanks a lot