Let's say I have a signal that I want to make available to several components of my program throughout the entire project. Let's also say there might be more than one component that might trigger or emit that signal.
Does it make sense to keep a global instance or a singleton of a class that manages such "global" signals?
I mean like this:
class SignalCollection
: public QObject{
Q_OBJECT
void EmitLogMsg
(QString msg
) {emit sigLogMsg
(msg
);
} void EmitSettChange() {emit sigSettChange();}
signals:
void sigSettChange();
}
class SignalCollection: public QObject
{
Q_OBJECT
void EmitLogMsg(QString msg) {emit sigLogMsg(msg);}
void EmitSettChange() {emit sigSettChange();}
signals:
void sigLogMsg(QString msg);
void sigSettChange();
}
To copy to clipboard, switch view to plain text mode
Now receivers from anywhere can connect() to the signals with the SignalCollection as the sender. And senders from anywhere can just call EmitSettChange() from anywhere to emit such a signal.
The advantage is that this breaks up the relation between sender and receiver.
Is that a common pattern? Are there better ways to do this? Or are there any good reasons why I should NOT do that?
Bookmarks