QObject::sender()的用法

先从官方文档开始看起:

 [protected] QObject *QObject::sender() const

Returns a pointer to the object that sent the signal, if called in a
slot activated by a signal; otherwise it returns nullptr. The pointer
is valid only during the execution of the slot that calls this
function from this object’s thread context. The pointer returned by
this function becomes invalid if the sender is destroyed, or if the
slot is disconnected from the sender’s signal.

首先是告诉我们QObject::sender()获得的QObject *指针的有效性,在什么情况有效,什么情况下失效。

下面是两个Warning,写程序的时候,要注意Warning,看帮助文档也是这样:
(1)Warning: This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.
(2)Warning: As mentioned above, the return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object’s thread. Do not use this function in this type of scenario.
解读:
对第一个Warning的解读:这个函数违反了面向对象的模块化原则。然而,当许多信号连接到一个槽函数时,访问发送方可能是有用的。
警告的原因是这样做违反了面向对象的原则,直接可以获得发送这个信号的对象的指针(在槽函数中),当多个信号连接到一个槽函数的时候,槽函数要区分是谁发送的,这个就QObject::sender()就比较有效。

第二个Warning的解读:当通过Qt::DirectConnection从与此对象的线程不同的线程调用信号与槽时,此函数的返回值无效。不要在这种情况下使用此函数。这个又引出了另外一个问题,就是在跨线程连接的信号与槽时,第五个参数的作用。

[static] QMetaObject::Connection QObject::connect(
		const QObject *sender, 
		const char *signal, 
		const QObject *receiver, 
		const char *method,
 		Qt::ConnectionType type = Qt::AutoConnection)

第五个参数Qt::ConnectionType type = Qt::AutoConnection
默认是Qt::AutoConnection,当发送信号与槽函数处于同一个线程时,这个是

Constant Value Description
Qt::AutoConnection 0 (Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. Th
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值