PDA

View Full Version : QTimer doesnt work or complains about thread



daemonna
1st September 2010, 13:53
i got following code



class freez_session : public QThread
{
Q_OBJECT
public:
explicit freez_session(QObject *parent = 0);
void run();

private slots:
void uploadfullmap();
}

void freez_session::run()
{
QTimer *tr = new QTimer();
connect(tr, SIGNAL(timeout()), this, SLOT(uploadfullmap()));
tr->start(500);
...
}

void freez_session::uploadfullmap()
{
qDebug() << "uploading full map";
}


but uploadfullmap() will never trigger!!


when i use QTimer *tr = new QTimer(this);

i get QObject: Cannot create children for parent that is in different thread...

where in a thread should be timer put so it work properly?

wysota
1st September 2010, 14:11
Your first snippet is ok regarding the timer. The problem is probably that you're not running (or you are blocking) the event loop for the main thread which is required for slots in the freez_session object to work.

daemonna
1st September 2010, 15:42
despite i dont fully understand why it does/doesnt work, solution is following...

- i had to remove forever loop and replace it with another timer which will continue call a function 'forever'

so NO FOREVER/WHILE/FOR(;;) loops in thread if you want use timers..

wysota
1st September 2010, 16:00
despite i dont fully understand why it does/doesnt work, solution is following...
I told you why it didn't work. You were blocking the event loop.