Ok, thank you for the explanation. I've just tried the following code. It doesn't work if I call QThread::wait for the thread where worker is running, and that seems to be strange, because that thread must call exec and process all events.
// Worker declaration (worker.h)
{
Q_OBJECT
public:
Worker();
~Worker();
signals:
void finished();
public slots:
void start();
void socketConnected();
void socketError();
void socketDisconnected();
private:
};
Q_DECLARE_METATYPE(Worker*)
// usage (main.cpp)
int main(int argc, char *argv[])
{
qRegisterMetaType<Worker*>();
Worker *w = new Worker;
w->moveToThread(t);
bool con;
con
= QObject::connect(t,
SIGNAL(started
()), w,
SLOT(start
()));
con
= QObject::connect(w,
SIGNAL(finished
()), t,
SLOT(quit
()));
t->start();
t->wait(); // !!! this causes a problem
return a.exec();
}
// Worker declaration (worker.h)
class Worker : public QObject
{
Q_OBJECT
public:
Worker();
~Worker();
signals:
void finished();
public slots:
void start();
void socketConnected();
void socketError();
void socketDisconnected();
private:
QTcpSocket sock;
};
Q_DECLARE_METATYPE(Worker*)
// usage (main.cpp)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaType<Worker*>();
QThread *t = new QThread;
Worker *w = new Worker;
w->moveToThread(t);
bool con;
con = QObject::connect(t, SIGNAL(started()), w, SLOT(start()));
con = QObject::connect(w, SIGNAL(finished()), t, SLOT(quit()));
t->start();
t->wait(); // !!! this causes a problem
return a.exec();
}
To copy to clipboard, switch view to plain text mode
As far as I see event processing in the worker's thread doesn't affect anything, but the 'outer' thread does (here it is the main thread). I tried to change the code and place it inside of the other thread:
// this is how outer thread works
void MyThread::run()
{
qRegisterMetaType<Worker*>();
Worker *w = new Worker;
w->moveToThread(t);
bool con;
con
= QObject::connect(t,
SIGNAL(started
()), w,
SLOT(start
()));
con
= QObject::connect(w,
SIGNAL(finished
()), t,
SLOT(quit
()));
con
= QObject::connect(t,
SIGNAL(finished
()),
this,
SLOT(quit
()));
t->start();
exec();
}
// and this is how MyThread is used
int main(int argc, char *argv[])
{
MyThread *t = new MyThread;
t->start();
t->wait();
return a.exec();
}
// this is how outer thread works
void MyThread::run()
{
qRegisterMetaType<Worker*>();
QThread *t = new QThread;
Worker *w = new Worker;
w->moveToThread(t);
bool con;
con = QObject::connect(t, SIGNAL(started()), w, SLOT(start()));
con = QObject::connect(w, SIGNAL(finished()), t, SLOT(quit()));
con = QObject::connect(t, SIGNAL(finished()), this, SLOT(quit()));
t->start();
exec();
}
// and this is how MyThread is used
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread *t = new MyThread;
t->start();
t->wait();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The last code works, I think because of QThread::exec called inside MyThread::run. But I thought that event processing must be performed by that thread where I moved my Worker object (called t in my case). Could you clarify this, please?
Bookmarks