Originally Posted by
Talei
Did You test that code?
I'm using Win with qt 4.8.1 and that QTimer don't work for me.
of course it doesn't - you didn't put an event loop in there anywhere! Additionally, the slot will only be invoked from the main thread, not the thread that run() is in!
Added after 14 minutes:
Originally Posted by
Talei
And why I shouldn't give parent to the QThread
sorry, that is fine. I was thinking of doing moveToThread(this) in a thread ctor which causes more hassle.
because that makes the qtimer associated to the main thread - but you want it in the new thread.
Added after 12 minutes:
maybe this will make things clearer.
// winthread.h
#ifndef WNTHREAD_H
#define WNTHREAD_H
#include <QThread>
#include <QTimer>
#include <QDebug>
{
Q_OBJECT
public slots:
void shout()
{
static int i = 0;
qDebug() << "shout " << i++ << " " << thread();
}
};
{
Q_OBJECT
public:
qDebug() << "wnThread ctor:" << this->currentThread();
}
~wnThread();
void run() {
qDebug() << "in thread th b4 exec:" << this->currentThread();
Shout s;
connect(&t, SIGNAL(timeout()), this, SLOT(doSomeWork())); // 'this' belongs to main thread!
connect(&t, SIGNAL(timeout()), &s, SLOT(shout())); // s belongs to this new thread
qDebug() << "timer is in " << t.thread();
t.start(3000);
exec(); // <-- enter event loop and wait until quit() or exit()
qDebug() << "after exec:" << this->currentThread();
}
public slots:
void doSomeWork() {
static int x = 0;
++x;
int c = 0;
qDebug() << "doSomeWork " << thread() << currentThread();
while (c < 1000) {
c++;
}
if (x > 5)
this->quit();
}
};
#endif // WNTHREAD_H
#ifndef WNTHREAD_H
#define WNTHREAD_H
#include <QThread>
#include <QTimer>
#include <QDebug>
class Shout : public QObject
{
Q_OBJECT
public slots:
void shout()
{
static int i = 0;
qDebug() << "shout " << i++ << " " << thread();
}
};
class wnThread : public QThread
{
Q_OBJECT
public:
explicit wnThread(QObject *parent = 0) : QThread(parent) {
qDebug() << "wnThread ctor:" << this->currentThread();
}
~wnThread();
void run() {
qDebug() << "in thread th b4 exec:" << this->currentThread();
QTimer t;
Shout s;
connect(&t, SIGNAL(timeout()), this, SLOT(doSomeWork())); // 'this' belongs to main thread!
connect(&t, SIGNAL(timeout()), &s, SLOT(shout())); // s belongs to this new thread
qDebug() << "timer is in " << t.thread();
t.start(3000);
exec(); // <-- enter event loop and wait until quit() or exit()
qDebug() << "after exec:" << this->currentThread();
}
public slots:
void doSomeWork() {
static int x = 0;
++x;
int c = 0;
qDebug() << "doSomeWork " << thread() << currentThread();
while (c < 1000) {
c++;
}
if (x > 5)
this->quit();
}
};
#endif // WNTHREAD_H
To copy to clipboard, switch view to plain text mode
main
#include <QtGui/QApplication>
#include "wnthread.h"
#include <QWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
w.show();
wnThread *th = new wnThread();
th->start();
return a.exec();
}
#include <QtGui/QApplication>
#include "wnthread.h"
#include <QWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.show();
wnThread *th = new wnThread();
th->start();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks