是否允许通过指向另一个 class 实例的指针发出信号?

Is it allowed to emit a signal trough a pointer to an instance of another class?

最小示例:

class Foo : public QObject
{
    Q_OBJECT

signals:

    void
    TestSignal(int i) const;
};

class Bar : public QObject
{
    Q_OBJECT

public:

     Bar(Foo* foo) :
         mFoo{ foo }
     {}

    void
    TestEmit(int i) const
    {
        emit mFoo->TestSignal(i);
    }

private:

    Foo* mFoo;
};

void
Print(int i)
{
    std::cout << i << std::endl;
}

用法:

Foo aFoo;
Bar aBar{ &aFoo };

connect(&aFoo, &Foo::TestSignal, &Print);

aBar.TestEmit(1337);

所以我使用指向 Foo 实例的指针从函数 Bar::TestEmit 发出信号 Foo::TestSignal。这似乎工作正常,但它被允许吗? (如:可靠的定义行为)。

来自 https://doc.qt.io/qt-5/signalsandslots.html :

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

我理解它在技术上是允许和可靠的,但在代码设计方面不推荐。

您可能也有兴趣将一个信号连接到另一个信号,如此处所述https://doc.qt.io/qt-5/qobject.html#connect