• 4016阅读
  • 2回复

请问下如何知道一个信号现在有几个槽连接着? [复制链接]

上一主题 下一主题
离线yj_yulin
 

只看楼主 倒序阅读 楼主  发表于: 2008-11-30
— 本帖被 XChinux 执行加亮操作(2009-01-06) —
如何知道一个信号现在有几个槽连接着?
因为关系到对象的生成和生存期管理,如果知道了没有连接,可以不生成额外的对象.
离线yj_yulin

只看该作者 1楼 发表于: 2008-12-01
已经解决.
int QObject::receivers ( const char * signal ) const  [protected]
Returns the number of receivers connected to the signal.
Since both slots and signals can be used as receivers for signals, and the same connections can be made many times, the number of receivers is the same as the number of connections made from this signal.
When calling this function, you can use the SIGNAL() macro to pass a specific signal:
        if (receivers(SIGNAL(valueChanged(QByteArray))) > 0) {
            QByteArray data;
            get_the_value(&data);      // expensive operation
            emit valueChanged(data);
        }
As the code snippet above illustrates, you can use this function to avoid emitting a signal that nobody listens to.
Warning: This function violates the object-oriented principle of modularity. However, it might be useful when you need to perform expensive initialization only if something is connected to a signal.
离线lazybone
只看该作者 2楼 发表于: 2008-12-01
快速回复
限100 字节
 
上一个 下一个