[solved] Is it safe to emit other instance's signal(s) ?
-
I have client code like
@
class Hsm : public QObject
{
...
void handleInitEvent();
};void Hsm::handleInitEvent()
{
emit gui->initCounter();
}
@and the signals' owner was written like this.
@
class GuiSignals : public QObject
{
...
signals:
#ifndef Q_MOC_RUN
public: // don't tell moc, doxygen or kdevelop, but those signals are in fact public
#endif
void initCounter(int value=10);
};
extern GuiSignals* gui; // assume this is instantiated beforehand somehow.
@Please be noted that I introduced Q_MOC_RUN non-define'd region and put 'public' keyword there to make it compilable.
(I've not been aware 'signals' keyword is defined as 'protected:' till now :-( )
So far, slot gets be called but this is a kind of hack looking, and feels not that good. Is there any special reason that a signal is not in public? -
In principle, there can be good reasons for doing what you are doing, and I don't think it is wrong per-se. However, the fact that you need hacks to do it, might already indicate that you are in fact doing something non-standard.
If possible without completely screwing up your overall design, I would recommend not to do this. Alternatively, create some methods on your GuiSignals class that allow triggering the slots. These may be public or perhaps protected (using fried like Tomma suggests).
As an alternative approach to qmake/moc hacks, you can also use QMetaObject to trigger signals from outside of the object itself.
@
QMetaObject::invokeMethod(gui, "initCounter");
@
And yes, that is also a hack.