1

スレッド化された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 であり、私を混乱させます。

4

1 に答える 1

2

あなたのメインコードで:

WirelessNet *wifi = new WirelessNet(0); // 0 = assign no parent
QThread *wifiThread = new QThread;
wifi->moveToThread(wifiThread);
QObject::connect(wifiThread, SIGNAL(started()), wifi, SLOT(startWifi()));
// start() will start its own event loop, it will emit started(), therefore startWifi() slot will be called.
wifiThread->start();

次に、WirelessNet クラス ヘッダー:

class WirelessNet : public QTcpServer
{
    Q_OBJECT

public:
    WirelessNet(QObject *parent = 0);

protected:
    void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;

public slots:
    void startWifi();
};

次に、WirelessNet クラス本体:

WirelessNet::WirelessNet(QObject *parent) : 
    QTcpServer(parent)
{
    // Do nothing much here because we want to initialise new stuff in our thread.
    // When this function runs we have not moved this to the new thread - or even started it.
}

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();
}

// Called when the thread has started
void WirelessNet::startWifi()
{
    // Anything done here is now safely within out new thread.
    listen(QHostAddress::Any, 5220);
    printf("is listening %d\n", this->isListening());
}

これはサンプルコードであり、コンパイルされていないスタックオーバーフローに直接書き込んだため、おそらくいくつかのエラーがあることに注意してください:)コメントした重要なポイントがいくつかありますが、元の試みで間違っていた可能性があります。

于 2016-03-30T11:16:53.233 に答える