QThread/QObject and QTimer
I'm using QTimer in QThread( or QObject ) class :
Code:
class InfoThread: public QThreed
{
Q_OBJECT
...
void run();
signals:
void newInfoAvalible( /* some data */ );
private slots:
void update();
..
private:
};
{
connect( p_timer, SIGNAL( timeout() ), this, SLOT( update() ) );
}
void InfoThread::run()
{
p_timer->start( 1000 );
exec(); // event loop for QTimer
}
void InfoThread::update()
{
...
emit newInfoAvalible( /* some data */ );
}
For me code is good, but signal newInfoAvalible( /* some data */ ) causes apps to crash. Wher's the problem?
Re: QThread/QObject and QTimer
Is newInfoAvalible() connected to a slot in another QThread?
If so, does the /*some data*/ contain pointers to something which are invalid after you leave this function? If so, this is expected, as signals between threads use "queued connections".
Re: QThread/QObject and QTimer
The QTimer object must be created in InfoThread::run() without a parent (preferably on the stack since QThread::exec() blocks) and the connection must be forced as direct
Code:
connect( &timer, SIGNAL( timeout() ), this, SLOT( update() ), Qt::DirectConnection );
because QThread itself lives in different thread than what's being executed in QThread::run().