スレッド化されたqt-networkingに関するチュートリアル(ここにあります:http://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html)に固執し、いくつかの小さな変更を加えてメインプログラムに統合しました. ただしincomingConnection()
、実行されることはありませんが、クライアントは接続できます。私はincomingConnection()で作業したいので、 で作業するのは時代遅れになりましたSIGNAL(newConnection())
が、これでも機能していません。
誰かが何がうまくいかないのか知っていますか?
ここに私の.h
#include <QtNetwork>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>
class WirelessNetThread: public Thread
{
Q_OBJECT
public:
WirelessNetThread(int socketDescriptor, QObject * parent);
void run() Q_DECL_OVERRIDE;
signals:
void error(QTcpSocket::SocketError socketError);
private:
int socketDescriptor;
QString text;
};
class WirelessNet : public QTcpServer
{
Q_OBJECT
public:
WirelessNet(QObject *parent = 0);
protected:
void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
};
そしてその.cpp
WirelessNetThread::WirelessNetThread(int socketDescriptor, QObject *parent):QThread(parent), socketDescriptor(socketDescriptor)
{
}
void WirelessNetThread::run()
{
QTcpSocket tcpSocket;
if ( !tcpSocket.setSocketDescriptor(socketDescriptor))
{
emit error(tcpSocket.error());
return;
}
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
WirelessNet::WirelessNet(QObject *parent): QTcpServer(0)
{
listen(QHostAddress::Any, 5220);
printf("is listening %d\n", this->isListening());
}
void WirelessNet::incomingConnection(qintptr socketDescriptor)
{
qDebug() << "incomming \n";
printf("incomming \n");
WirelessNetThread *thread = new WirelessNetThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
ここで、メインプログラムからの抜粋です。ここで、それが開始されます (ちなみに、 moveToThread()を省略しても問題ありません:
WirelessNet *wifi = new WirelessNet(this->parent());
wifi->moveToThread(this->thread());
wifiの初期化後にこれらの行を追加しても、これでも影響はありません。
wifi = new WirelessNet(this->parent());
QEventLoop testLoop;
testLoop.exec();
つまり、「incomming」は印刷されないため、作業できません。これは、チュートリアルのコードとほぼ 1 対 1 であり、私を混乱させます。