private/public qt signals

前端 未结 7 2278
情话喂你
情话喂你 2020-12-10 00:36

Can Qt signals be public or private? Can I create internal signals, which are seen only inside the class?

Update: I have a class with some internal

7条回答
  •  猫巷女王i
    2020-12-10 01:16

    A common way, e.g. seen in kdelibs, is this:

    Q_SIGNALS:
    #ifndef Q_MOC_RUN
        private: // don't tell moc, doxygen or kdevelop, but those signals are in fact private
    #endif
    
       void somePrivateSignal();
    

    This makes the signal private, i.e. it can only be emitted by the class itself but not by its subclasses. To not make the "private:" overrule Q_SIGNALS (moc wouldn't see somePrivateSignal as signal then), it's inside Q_MOC_RUN, which is only defined when moc runs.

    Edit: This approach doesn't work for the new-style connects introduced with Qt 5 (connect(a, &A::someSignal, b, &B::someSlot)), as they require the signal to be accessible.

提交回复
热议问题