1

1 つのスロットを受信する 2 つ (またはそれ以上) の同じシグナルを送信していますが、2 回ではなく 1 回しか呼び出されません。

main.cpp:

#include <QCoreApplication>
#include "app.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    App app;

    QMetaObject::invokeMethod(&app, "run", Qt::QueuedConnection);

    return a.exec();
}

app.h:

#ifndef APP_H
#define APP_H

#include <QObject>
#include "tcpserver.h"
#include "tcpsocket.h"

class App : public QObject
{
        Q_OBJECT
    public:
        explicit App(QObject *parent = 0);

    signals:

    public slots:
        void run()
        {
            qDebug() << "run()";
            server.server_start(1111);
            socket.connectToHost("127.0.0.1", 1111);
            socket.write("hello", 5);
            socket.write("olleh", 5); // should execute slot two times.
        }

    private:
        TcpServer server;
        TcpSocket socket;

};

#endif // APP_H

TcpSocket.h:

#ifndef TCPSOCKET_H
#define TCPSOCKET_H

#include <QTcpSocket>

class TcpSocket : public QTcpSocket
{
        Q_OBJECT
    public:
        explicit TcpSocket(QObject *parent = 0);

    signals:
        void dataReady(QByteArray data);

    public slots:
        void readyRead()
        {
            qDebug() << "Bytes available:" << this.bytesAvailable(); // called only once.
            data = this.readAll(); // just for testing.
            emit dataReady(data);  //
        }
        void disconnected();

    private:
        QByteArray data;

};

#endif // TCPSOCKET_H

ご覧のとおり、私は 2 つの socket.write 関数を実行しています。これは 2 つの readyRead スロットを処理する必要がありますが、呼び出されるのは 1 回だけです。正直なところ、自分が間違っていることを理解していません。

よろしく。

4

0 に答える 0