关于QT中跨线程的信号处理问题,求大神!

浮世尘雲 2013-12-04 08:35:39
说明如下:
1. Receiver对象生存在主线程,主线程拥有事件循环;
2. Receiver对象生存期足够长;
3. connect函数调用成功,连接类型缺省为autoConnection, 对于跨线程的信号,将会自动使用队列连接;
4. 信号确实发送,emit threadSignal();语句确实执行了;
5. Receiver对象所在线程(主线程)界面响应正常,事件循环没有阻塞;
6. 问题是槽函数从来都没有执行过?纠结其原因。。。

望大神解惑


class MyThread : public QThread
{
Q_OBJECT

public:
MyThread();

void run()
{
emit threadSignal();
}

signals:
void threadSignal();
};


class Receiver : public QObject
{
Q_OBJECT

public:
Receiver()
{
MyThread * thread = new MyThread;
connect(thread, SIGNAL(threadSignal()), this, SLOT(slotFunc()));
thread->start();
}

private slots:
void slotFunc()
{
qDebug() << "receive one signal.";
}
};

...全文
122 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
浮世尘雲 2013-12-04
  • 打赏
  • 举报
回复
其实上面这段代码是可以发出信号的,而且槽函数也可以异步被调用。 我遇到的问题与上面类似,只不过是槽函数及信号原型中含有 win32 下的 typedef 类型 HANDLE, 结果这种基于队列连接的异步调用就失败了,我把原型中的HANDLE 类型改成 void * 类型 问题就解决了,虽然我到现在也不知道为什么会这样,但好歹问题解决了,谢谢楼上回复。 我的QT版本是 4.8.4。
ppdayz 2013-12-04
  • 打赏
  • 举报
回复
引用 楼主 leeyunce 的回复:
说明如下: 1. Receiver对象生存在主线程,主线程拥有事件循环; 2. Receiver对象生存期足够长; 3. connect函数调用成功,连接类型缺省为autoConnection, 对于跨线程的信号,将会自动使用队列连接; 4. 信号确实发送,emit threadSignal();语句确实执行了; 5. Receiver对象所在线程(主线程)界面响应正常,事件循环没有阻塞; 6. 问题是槽函数从来都没有执行过?纠结其原因。。。 望大神解惑

class MyThread : public QThread
{
    Q_OBJECT

public:
    MyThread();

    void run()
    {
        emit threadSignal();
    }

signals:
    void threadSignal();
};


class Receiver : public QObject
{
    Q_OBJECT

public:
    Receiver()
    {
        MyThread * thread = new MyThread;
        connect(thread, SIGNAL(threadSignal()), this, SLOT(slotFunc()));
        thread->start();
    }

private slots:
    void slotFunc()
    {
        qDebug() << "receive one signal.";
    }
};

其实你可以去看下Qt的帮助文档是怎么实现多线程的,看了你就懂了。 http://qt-project.org/doc/qt-5.0/qtcore/qthread.html

16,239

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧