// create a queued connection between the thread and the parent
Thread* thread = new Thread(this);
connect(this, SIGNAL(somethingHappened()), thread, SLOT(closeConnection()), Qt::QueuedConnection);
// ..and when you would call closeConnectionNow(), now you will just emit somethingHappened() instead
// remember to declare somethingHappened() as a signal
emit somethingHappened();
// declare Thread::closeConnectionNow() as a slot
void Thread::closeConnectionNow()
{
socket->disconnectFromHost();
}
void Thread::run()
{
...
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(disconnected()), this, SLOT(closeConnection()));
exec(); // enter to the event loop
}
// create a queued connection between the thread and the parent
Thread* thread = new Thread(this);
connect(this, SIGNAL(somethingHappened()), thread, SLOT(closeConnection()), Qt::QueuedConnection);
// ..and when you would call closeConnectionNow(), now you will just emit somethingHappened() instead
// remember to declare somethingHappened() as a signal
emit somethingHappened();
// declare Thread::closeConnectionNow() as a slot
void Thread::closeConnectionNow()
{
socket->disconnectFromHost();
}
void Thread::run()
{
...
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(disconnected()), this, SLOT(closeConnection()));
exec(); // enter to the event loop
}
To copy to clipboard, switch view to plain text mode
Bookmarks