Qt信号和槽:权限 [英] Qt signals and slots: permissions

查看:200
本文介绍了Qt信号和槽:权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里SO和实际的Qt文档之间存在差异。

There are discrepancies between respected answers here on SO and the actual Qt docs.

我读过这个问题,我想进一步澄清。任何人都可以确认:

I've read this question and I want some further clarification. Can anyone confirm:


  • 信号总是 protected 仅由类或其任何子类。我不知道这是真的;上面的问题显示了支持这种说法的答案。但 Qt文档说:信号是公共访问函数,可以从任何地方发射,但我们建议只从定义信号及其子类的类中发射它们。那么是什么呢?

  • 功能,因此可以是公共的,私人的或受保护的。显然,外部类有能力控制,如果你的类将其自己的信号之一连接到其自己的插槽之一,如果插槽是公共的。然而,SO信息与文档不同,它说:从任意类的实例发出的信号可以导致在不相关类的实例中调用私有时隙。这意味着 private 不受信号/槽机制支持?

  • public,private,protected使用信号关键字

  • 发出的信号始终可用于所有其他类,

  • 尽管所有类都可以查看所有信号,但仍然可以有两个类由于 connect 函数将类名称作为信号前缀(即 SomeClass :: itsSignal

  • A signal is always protected, therefore it can be emitted only by the class or any of its subclasses. I'm not sure this is true; the question above shows answers supporting this statement. But the Qt docs say: 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. So which is it?
  • Slots are just functions, and thus may be public, private or protected. Obviously an outside class will have the ability to control if your class connects one of its own signals to one of its own slots if the slot is public. However, again the SO information differs from the docs, which say: 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. This means that private is not honored by the signal/slot mechanism?
  • The words public, private, protected have no use with working with the signal keyword
  • The emitted signal is always available to all other classes, that is, any other class may always connect to that signal (regardless of its permission to emit the signal).
  • Despite that all signals are viewable to by all classes, you could still have two classes with signals of the same name since the connect function takes the class name as a signal prefix (i.e. SomeClass::itsSignal)

推荐答案


  • 信号在Qt4中受到保护,

  • 插槽是函数,而public / protected / private在调用它们时,当连接到信号时,元对象系统忽略它。

  • 由于信号定义为 public:

    • Signals are protected in Qt4 but are public in Qt5, thus the contradictory information.
    • Slots are functions and public/protected/private is honored when calling them as such, when connecting to a signal, the metaobject system ignores it though.
    • As signals is defined as public:, prepending them with e.g. private leads
    • 到:

      private:
      public: //signals:
          void theSignal();
      

      因此无效。


      • 所有类都可以连接到任何信号,正确。在这方面,信号是公共API的一部分。

      • 具有相同的信号签名不是问题。上下文由指定为发件人的对象定义。

      使用旧式连接:

      Apple *apple ... Orange* orange
      connect(apple, SIGNAL(changed()), this, SLOT(appleChanged()));
      connect(orange, SIGNAL(changed()), this, SLOT(orangeChanged()));
      

      信号在这里被指定为string(没有类名),但是 apple orange 只有一个信号 changed()在每个类(不是实例)存在一个QObject实例的元对象,它们不能冲突。

      The signal is specified as string here (without the class name in it), but as apple and orange have only one signal changed() each and the lookup is done in the metaobject of the QObject instance, which exists one per class (not instance), they cannot collide.

      Qt 5版本与编译时检查: p>

      Qt 5 version with compile-time checking:

      connect(apple, &Apple::changed, this, &MyReceiver::appleChanged);
      

      这里必须指定一个函数,所以根据范围,必须指定一个类名也许命名空间)。作为一个模糊的函数名不会是有效的C ++,因此不能编译,所以一个是安全的。

      Here one must specify a function, so depending on the scope, one must specify a class name (and maybe namespaces). As an ambiguous function name wouldn't be valid C++ and thus not compile, so one is safe here.

      这篇关于Qt信号和槽:权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆