1

我正在发送两个(或更多)相同的信号,它接收一个插槽,但它只被调用一次而不是两次。我做错了什么?

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

应用程序.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

如您所见,我正在执行两个 socket.write 函数,应该处理两个 readyRead 插槽,但它只被调用一次。老实说,我不明白我做错了什么。

问候。

4

0 回答 0