表现方式 Qt中Signal Slot 4 种连接

移动开发
本文介绍的是表现方式 Qt中Signal Slot 4 种连接,很详细对为友们讲解,先来看内容。

表现方式 Qt Signal Slot 4 种连接是本文介绍的内容,先来看内容。signal/slot在底层会使用三种方式传递消息。参见QObject::connect()方法:

bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection )

最后一个参数是就是传递消息的方式了,有四个取值:

Qt::DirectConnection

When emitted, the signal is immediately delivered to the slot.

假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这4个slot按连接的时间顺序调用一遍。显然这种方式不能跨线程(传递消息)。

Qt::QueuedConnection

When emitted, the signal is queued until the event loop is able to deliver it to the slot.

假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这个signal包装成一个 QEvent,放到消息队列里。QApplication::exec()或者线程的QThread::exec()会从消息队列里取消息,然后调用 signal关联的几个slot。这种方式既可以在线程内传递消息,也可以跨线程传递消息。

Qt::BlockingQueuedConnection

Same as QueuedConnection, except that the current thread blocks until the slot has been delivered. This connection type should only be used for receivers in a different thread. Note that misuse of this type can lead to dead locks in your application.

与Qt::QueuedConnection类似,但是会阻塞等到关联的 slot 都被执行。这里出现了阻塞这个词,说明它是专门用来多线程间传递消息的。

Qt::AutoConnection

If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection.

这种连接类型根据signalslot是否在同一个线程里自动选择Qt::DirectConnection或Qt::QueuedConnection

这样看来,第一种类型的效率肯定比第二种高,毕竟第二种方式需要将消息存储到队列,而且可能会涉及到大对象的复制(考虑sig_produced(BigObject bo),bo需要复制到队列里)。

与wxWidget、MFC的效率比较

wxWidget没用过,了解过MFC。我觉得MFC的消息队列是比较简单的,一般只是传递long数值、HANDLE、指针,效率上应该会比QT的队列方式高,然而QT的灵活性是MFC没办法比拟的,我还是比较喜欢QT这样的。

小结:表现方式 Qt Signal Slot 4 种连接的内容介绍完了,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-01 17:50:13

Python 多线程

2011-08-03 16:26:14

iPhone UIView 动画

2011-08-03 16:35:10

iPhone UIView 动画

2011-06-17 09:58:26

Qt Chapter QObject

2017-04-19 08:32:50

大数据数据可视化编程工具

2011-06-23 14:40:13

Qt 信号

2011-06-23 13:38:27

QT 元对象 信号

2014-12-31 14:09:23

xml解析

2009-08-22 17:08:02

家庭智能布线综合布线连接

2019-11-27 18:43:36

程序员编程语言软件

2017-08-31 15:20:03

PythonPython3HTTP

2018-04-28 15:51:33

Mybatis方式传递

2019-12-04 12:52:07

物联网数据分析IOT

2011-04-13 14:38:17

2018-02-05 08:36:22

NetAppVeeam AWS存储

2021-01-08 10:52:22

物联网万物互联IoT,Interne

2019-12-16 10:13:16

Python字符串数据

2011-06-08 11:15:21

web.configASP.NET

2010-03-18 11:06:18

Python stuc

2011-05-20 09:55:26

Oracle连接
点赞
收藏

51CTO技术栈公众号