SO上的受尊重答案与实际Qt文档之间存在差异。
我已经阅读了这个问题,希望进一步澄清。任何人都可以确认:
protected
,因此只能由该类或其任何子类发出。我不确定这是真的,上面的问题显示了支持此声明的答案。但是Qt文档说:Signals are public access functions and can be emitted from anywhere, but we recommend to only emit them from the class that defines the signal and its subclasses.
那是什么?a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.
这意味着private
信号/插槽机制不遵守吗?signal
关键字无关connect
函数将类名作为信号前缀(即SomeClass::itsSignal
)signals
被定义为public:
,如与私人引线预先考虑他们至:
private:
public: //signals:
void theSignal();
因此,它没有效果。
使用旧式连接:
Apple *apple ... Orange* orange
connect(apple, SIGNAL(changed()), this, SLOT(appleChanged()));
connect(orange, SIGNAL(changed()), this, SLOT(orangeChanged()));
该信号在这里被指定为字符串(其中没有类名),但是由于apple
并且每个orange
信号只有一个,changed()
并且在QObject实例的元对象中进行查找,该对象在每个类(而不是实例)中都存在,所以它们不会发生冲突。 。
具有编译时检查功能的Qt 5版本:
connect(apple, &Apple::changed, this, &MyReceiver::appleChanged);
这里必须指定一个函数,因此根据范围,必须指定一个类名(可能还有名称空间)。由于函数名称不明确,因此C ++无效,因此无法编译,因此这里很安全。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句