在PictureBook Connector里初始使用的线程结构为
// SocketThread.h
SocketThread : QThread
{
protected:
virtual run();
};
// SocketThread.cpp
void SocketThread :: run()
{
QTcpSocket tcpSocket;
if(!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
// can't connect slot
// connect(&tcpSocket, SIGNAL(disconnected()), this, SLOT(quit()), Qt::QueuedConnection);
tcpSocket.moveToThread(this);
while( !m_quit && tcpSocket.state() == QAbstractSocket::ConnectedState ) {
// ...
}
}
因为需求的原因,需要将代码中的connect注释掉的代码打开,以使得在socket断掉的时候可以将thread退出。
但是却发现不能触发信号与槽,在一位同行的介绍下,在QtHelp里看了Multithreading Technologies in Qt。
Lifetime of thread | Operation | Solution |
---|---|---|
One call | Run a new linear function within another thread, optionally with progress updates du |