Qt两个或多个相同的信号只调用一次插槽

时间:2013-06-12 11:16:30

标签: qt console signals

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

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

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

问候。

0 个答案:

没有答案