Wednesday, November 22, 2017

Emit signals of other objects...

It'll depend on the Qt version you're using. If Qt 5.x it's easy, as signals are public methods. And the 'emit' keyword expands to an empty string, so just both entries below are equivalent:
a.signal_X();
emit a.signal_X();
If using Qt 4.x, it's not that easy since signals are protected methods so you can't emit signals directly. However, there are a couple of approaches to achieve that:
1. create public method in class A, that will emit necessary signals
2. create signal in class B and connect it to a signal in class A

Given all that said, please remember that it might not be a good idea to emit signals of other objetcs directly.

IMO signals are intended to be emitted by a class as a response to some event, i.e. a QPushButton object will emit the clicked() signal when the user clicks with the mouse on such button. So if you really needs that button to emit the clicked() signal, my approach is to provoke the right event so that signal is finally emitted. Following this example, you need to call QPushButton.click() method instead of emitting the signal directly, but having the exact same behavior as a result of the object's reaction to a particular event.

Happy coding!

No comments:

Post a Comment