zoukankan      html  css  js  c++  java
  • Qt 使用 lambda 表达式做为槽函数时为什么使用 QObject::sender() 获取到的发送信号对象指针为空?

    /*!
        Returns a pointer to the object that sent the signal, if called in
        a slot activated by a signal; otherwise it returns 0. 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.
    
        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.
    
        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.
    
        sa senderSignalIndex(), QSignalMapper
    */
    
    QObject *QObject::sender() const
    {
        Q_D(const QObject);
    
        QMutexLocker locker(signalSlotLock(this));
        if (!d->currentSender)
            return 0;
    
        for (QObjectPrivate::Connection *c = d->senders; c; c = c->next) {
            if (c->sender == d->currentSender->sender)
                return d->currentSender->sender;
        }
    
        return 0;
    }
    

    使用 lambda 表达式做槽函数时,相当于使用了 Qt::DirectConnection 方式连接槽,都在同一个线程中,故获取发送信号对象指针时,直接给你返回了 0.

  • 相关阅读:
    Log4net.config
    ASCII 转换帮助类
    维吉尼亚加密与解密
    nginx配置说明
    验证码
    css 设置下拉菜单
    输出一张自定义文字的图片
    mvc 自定义分页控件
    【模块化】export与export default在一个文件里共存,引入时需注意的地方
    【uniapp】兼容刘海屏底部安全区
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/10445454.html
Copyright © 2011-2022 走看看