You don't need a separate thread for that... you can do this all in one thread. If you want to "call" methods of a separate thread from the "main" thread, it involves synchronisation between those two. You can use signals & slots for that and then it is as easy as connecting them and emitting a signal. But remember that it is all an event driven mechanism and two threads don't wait for each other here...
Some small partial example (this is not a Newbie section, so I expect you can fill the rest).
public:
void run(){
connect(this, SIGNAL(sendToSocket(...)), sock, SLOT(...)));
// ...
exec();
}
// ...
};
MyThread *thr1;
//...
connect(thr1, SIGNAL(messageReceived(...)), this, SLOT(onMessageReceived(...)));
connect(this, SIGNAL(sendToThread(...)), thr1, SIGNAL(SendToSocket(...)));
thr1->start();
class MyThread : public QThread{
public:
void run(){
QTcpSocket *sock = new QTcpSocket;
connect(this, SIGNAL(sendToSocket(...)), sock, SLOT(...)));
// ...
exec();
}
// ...
};
MyThread *thr1;
//...
connect(thr1, SIGNAL(messageReceived(...)), this, SLOT(onMessageReceived(...)));
connect(this, SIGNAL(sendToThread(...)), thr1, SIGNAL(SendToSocket(...)));
thr1->start();
To copy to clipboard, switch view to plain text mode
Bookmarks