I would like to protest against a blind argument that subclassing QThread doesn't make sense anymore. Let's consider the prime number situation. It's much easier to do this:
class PrimeThread
: public QThread { Q_OBJECT
public:
void run() { for(int i=1;i<1000;i++) if(isPrime(i)) emit prime(i); }
signals:
void prime(int);
//...
};
PrimeThread thread;
thread.start();
class PrimeThread : public QThread {
Q_OBJECT
public:
void run() { for(int i=1;i<1000;i++) if(isPrime(i)) emit prime(i); }
signals:
void prime(int);
//...
};
PrimeThread thread;
thread.start();
To copy to clipboard, switch view to plain text mode
than this:
class PrimeObject
: public QObject { Q_OBJECT
public slots:
void run() {
for(int i=1;i<1000;i++) if(isPrime(i)) emit prime(i);
emit quit();
}
signals:
void prime(int);
void quit();
};
thread.start();
PrimeObject o;
connect(&o, SIGNAL(quit()), &thread, SLOT(quit()));
o.moveToThread(&thread);
QMetaObject::invokeMethod(&o,
"run", Qt
::QueuedConnection);
class PrimeObject : public QObject {
Q_OBJECT
public slots:
void run() {
for(int i=1;i<1000;i++) if(isPrime(i)) emit prime(i);
emit quit();
}
signals:
void prime(int);
void quit();
};
QThread thread;
thread.start();
PrimeObject o;
connect(&o, SIGNAL(quit()), &thread, SLOT(quit()));
o.moveToThread(&thread);
QMetaObject::invokeMethod(&o, "run", Qt::QueuedConnection);
To copy to clipboard, switch view to plain text mode
Bookmarks