How to emit a signal in whole of project?
-
Hi.
I receive data from QSocketNotifier in mainwindow.cpp . I want to emit signal when receive data in whole of application.
Then each class that they need this signal, call their own slot to do something.
How to define a global signal that work in whole of project? -
There is no "broadcast" mechanism to send a signal to every object. You have to connect every slot explicitely.
So you have to make your QSoketNotifier global accessible and every object can access the instance and connect to the signal himself if wanted.
(That's possible but not a clean solution - you should try to rework your code so that the signal emitting object does not need to know the receivers and the signal receiving objects do not need to know the emitter and some glue code initializes all instances and connects the signals and slots). -
Hello,
@micland Has provided some good points. I think however that the cleanest solution to a problem like this is to delegate the signals through the object hierarchy (and not to expose the original emitter to every object). So every parent object will have a signal, that the children will subscribe to, which in turn will raise the child's signal and so on.
For (a simple) example:class PropagatingSignalClass : public QObject { Q_OBJECT public: PropagatingSignalClass(QObject * parent) : QObject(parent) { PropagatingSignalClass * interestingParent = qobject_cast<PropagatingSignalClass *>(parent); if (interestingParent) QObject::connect(interestingParent, SIGNAL(someInterestingSignal()), this, SIGNAL(someInterestingSignal())); } signals: void someInterestingSignal(); }
When the top-level
QObject
's signal is triggered such structure will carry the signal down the object tree and isn't too hard to implement.Kind regards.
Create a class with singleton and observer pattern.
Why should he do that exactly? By the way a slot (connected to a signal) is already an observer pattern.