クライアントがシングルスレッドであるチャットプログラムに取り組んでいますが、サーバーは接続されたクライアントごとに新しいスレッドを開始します。私のクライアントコードはしっかりしていると思いますが、サーバーは私を当惑させています。
現在、QTcpSocket
着信接続を検索する派生クラスがあり、接続が検出されると、新しいを開始しQThread
ます。実行すると、 (チャットウィンドウである)QThread
のインスタンスが作成され、表示されます。QMainWindow
void secureserver::incomingConnection(int socketDescriptor)
{
securethread *thread = new securethread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
void securethread::run()
{
serverwindow myServerWindow;
myServerWindow.setSocketDescriptor(mySocket);
myServerWindow.show();
}
次のようなstderrorのエラーが発生していますが、QMainWindow
表示されないため、現時点ではチャットできません。
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0xbf9e5358), parent's thread is QThread(0x98a54f0), current thread is securethread(0x99e9250)
QPixmap: It is not safe to use pixmaps outside the GUI thread
私の質問は次のとおりです。
QThread
の親にする必要がありQMainWindow
ますか?- 私はこれを完全に間違った方法で行っていますか?
- これが期待どおりに機能しない理由や修正方法に関するその他の一般的なアイデアも大歓迎です。