Qt中的信号和槽之connect----多线程调用全解析(同步/异步)

学Qt也有一段时间了,,,可是对它的connection 函数却不是很了解,也没有考虑一些问题...

 

    比如,信号调用槽问题,是同步的,还是异步的:

 

    connect是4个参数吗??其实不是,它有5个参数,前4个都很熟悉,第五个参数是一个enum Qt::Connection Type类型的,它一共有六个值,下面是Qt Assistant中给出的明确说明,,

 

This enum describes the types of connection that can be used between signals and slots. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time.

 

ConstantValueDescription
Qt::AutoConnection0(default) Same as DirectConnection, if the emitter and receiver are in the same thread. Same as QueuedConnection, if the emitter and receiver are in different threads.
Qt::DirectConnection1The slot is invoked immediately, when the signal is emitted.
Qt::QueuedConnection2The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
Qt::BlockingQueuedConnection4Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads. Note: Violating this rule can cause your application to deadlock.
Qt::UniqueConnection0x80Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.
Qt::AutoCompatConnection3The default type when Qt 3 support is enabled. Same as AutoConnection but will also cause warnings to be output in certain situations. See Compatibility Signals and Slots for further information.

   这里各种类型后面都有详细说明,它给我们线程间通信提供了一种简单的实现形式,Qt又是开源的,所以,可以查看connect的源码,学习它

的异步调用方法.对于Qt::QueuedConnection和Qt::BlockQueuedConnection的实现.
         呵呵,有了这些知识,以后在用Qt信号的槽,尤其是写多线程时候,就好多了....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值