0

I have a Thread class thats derives from QThread:

class Thread :
    public QThread
{
    Q_OBJECT
public:
    Thread(QObject* parent = NULL);
    ~Thread(void);

    virtual void run();
    void stop();

public slots:
    virtual void OnMessage(const char* msg);

protected:
    bool m_Done;
};

And a derived class:

class DerivedThread: public Thread
{
    Q_OBJECT
public:
    virtual void run();

    ~DerivedThread();

public slots:
    virtual void OnMessage(const char* msg);

private:
    IQFeedSocket* m_Socket;
};

This is pretty straight forward. Then I have a Socket wrapper class that takes a Thread object in its constructor:

class IQFeedSocket : public QObject
{
    Q_OBJECT

public:
    IQFeedSocket(Thread *parent);
    ~IQFeedSocket();

    bool Connect(int port);
    bool Write(const char* msg);

public slots:
    void OnMessage();

signals:
    void SendMessage(const char* msg);

private:
    QTcpSocket* m_Socket;
    char* m_ReceiveBuffer;
};

Now, I do call exec() in my DerivedThread::run() method, and it is successful because I get the OnMessage() call in the socket object, which is instantiated within the thread. What I want to do is pretty simple: In the IQFeedSocket::OnMessage, I will check that the message receive is completed (it has to end in "\r\n") and when a complete message is received, forward it to my Thread object that is registered in the constructor. So, in my IQFeedSocket constructor, I do this:

IQFeedSocket::IQFeedSocket(Thread *parent)
    : QObject(parent)
{
    m_ReceiveBuffer = new char();
    connect(this,SIGNAL(SendMessage(const char*)), this->parent(), SLOT(OnMessage(const char*)));
}

And in the OnMessage() method:

void IQFeedSocket::OnMessage()
{
    const char* msg;
    if(m_Socket->bytesAvailable() != 0) //More stuff needed
    {
        QByteArray msgArray = m_Socket->readAll();
        msg = msgArray;
        emit SendMessage(msg);
    }
}

When I debug, I get to the emit line, but then the OnMessage(char*) method from the DerivedThread object is never called. I'm sure it's a pretty simple oversee, but I can't seem to spot where I'm going wrong. Any Ideas? thx!

The DerivedThread::OnMessage(char*):

void DerivedThread::OnMessage(const char* msg)
{
        //This never gets called :(
    printf(msg);
    char* f;
    strcpy(f,msg);
}

EDIT: I have also tried the connect() on the DerivedThread side:

m_Socket = new IQFeedSocket(this);
connect(m_Socket, SIGNAL(SendMessage(const char*)), this, SLOT(OnMessage(const char*)));
Thread::exec();

But with no luck.

4

1 に答える 1

1

シグナル/スロット定義の引数と接続呼び出しは、 を含めて完全に一致する必要がありconstます。

一方に typedef を使用し、もう一方に完全な名前を使用しても、それを壊すのに十分です。

于 2012-08-22T15:13:05.027 に答える