我正在尝试修改 Qt Fortune Threaded Server 示例以从连接中读取文本,然后将其回显。我在我的 FortuneThread.h 文件中定义了 tcpSocket,如下所示:
QTcpSocket tcpSocket;
我的线程新运行函数如下所示:
void FortuneThread::run()
{
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
connect(&tcpSocket, SIGNAL(readyREAD()), this, SLOT(readCommand()) );
}
哪个编译并运行,但是一旦我连接我得到这个错误(指连接线):
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x1eeb920), parent's thread is QThread(0x1bb3f90), current thread is FortuneThread(0x1eeb8f0)
QObject::connect: No such signal QTcpSocket::readyREAD() in ../fortune/fortunethread.cpp:60
有人可以向我解释原因吗?由于 tcpSocket 是在 FortuneThread 类(作为单独的线程运行)中定义的,并且“this”指的是 FortuneThread,我假设两个对象都在线程内?如何解决这个问题?