0

我有一个派生自 QThread 的 Thread 类:

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

和一个派生类:

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

    ~DerivedThread();

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

private:
    IQFeedSocket* m_Socket;
};

这很简单。然后我有一个 Socket 包装类,它在其构造函数中采用一个 Thread 对象:

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

现在,我确实在我的 DerivedThread::run() 方法中调用了 exec(),它是成功的,因为我在线程中实例化的套接字对象中获得了 OnMessage() 调用。我要做的很简单:在 IQFeedSocket::OnMessage 中,我将检查消息接收是否完成(它必须以“\r\n”结尾),当收到完整的消息时,将其转发到我的在构造函数中注册的线程对象。所以,在我的 IQFeedSocket 构造函数中,我这样做:

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

在 OnMessage() 方法中:

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

当我调试时,我到达了发射线,但是来自 DerivedThread 对象的 OnMessage(char*) 方法永远不会被调用。我确信这是一个非常简单的监督,但我似乎无法发现我哪里出错了。有任何想法吗?谢谢!

DerivedThread::OnMessage(char*):

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

编辑:我还尝试了 DerivedThread 端的 connect():

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

但没有运气。

4

1 回答 1

1

信号/槽定义和连接调用中的参数必须完全匹配,包括const.

即使在一个中使用 typedef 而在另一个中使用全名也足以破坏它。

于 2012-08-22T15:13:05.027 回答