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 を参照しているため、両方のオブジェクトがスレッド内にあると思いますか? これを修正する方法は?