Qt - 从c ++线程发出一个信号 [英] Qt - emit a signal from a c++ thread

查看:836
本文介绍了Qt - 从c ++线程发出一个信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Qt中的C ++线程(std :: thread)发出一个信号。



我该怎么办?

$ b $你肯定可以从线程发出一个信号( QThread std :: thread 或甚至 boost :: thread )。只有你必须小心你的连接函数的第五个参数( Qt :: ConnectionType ):



$ c> Qt :: DirectConnection :当信号被发出时,槽立即被调用(从当前线程)。
如果 Qt :: QueuedConnection :当控制返回到接收者线程的事件循环时,调用槽。该插槽在接收者的线程中执行。



请参阅 ConnectionType-enum 更多选项。



问题不是真正从哪个线程发出信号,更多从哪个线程调用插槽。例如,我认为 QLabel :: setText 必须从 QLabel 的所有者线程(很可能是主线程) 。所以如果你发出一个信号连接到 QLabel setText 从一个线程,连接必须用 Qt :: AutoConnection Qt :: QueuedConnection Qt :: BlockingQueuedConnection


I want to emit a signal from a C++ thread (std::thread) in Qt.

How can I do it?

解决方案

You definitely can emit a signal from a thread (QThread, std::thread or even boost::thread). Only you must be careful of your connect function's fifth parameter (Qt::ConnectionType):

If Qt::DirectConnection: The slot is invoked immediately (from the current thread), when the signal is emitted. If Qt::QueuedConnection: The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

See ConnectionType-enum for more options.

The problem is not really from which thread you emit the signal, it's more from which thread the slot is being invoked. For instance, I think QLabel::setText must be executed from QLabel's owner thread (most likely main thread). So if you emit a signal connected to a QLabel's setText from a thread, connection must be done with Qt::AutoConnection, Qt::QueuedConnection or Qt::BlockingQueuedConnection.

这篇关于Qt - 从c ++线程发出一个信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆