[solved] Is it safe to emit other instance's signal(s) ?
-
wrote on 16 Jul 2012, 06:09 last edited by
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? -
wrote on 16 Jul 2012, 07:17 last edited by
I would put GuiSignals as friend of Hsm if you need more of GuiSignals' signals emited from there. Or you can make public method GuiSignals::emitInitCounter(int) which handles emiting.
-
wrote on 16 Jul 2012, 08:00 last edited by
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. -
wrote on 16 Jul 2012, 09:13 last edited by
Thanks guys. Though not figure out the reason of that standard('protected' signal only), tips were helpful.
-
wrote on 16 Jul 2012, 09:26 last edited by
On a side note: to support the template-based signal-slot syntax in Qt5, AFAIK signals will be public there by default.
4/5