I'm interested to know if there is some way that I can determine that there is a connection to a given signal that I've defined in a class. It's basically a signal to process data, and I don't care what it's connected to, but I'd like to include a sanity check that I'm not sending data into the void where it will never be seen. I've checked out Determine signals connected to a given slot in Qt, but it's kind of the opposite problem.
A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them. Since slots are normal member functions, they follow the normal C++ rules when called directly.
If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted. Signals are automatically generated by the moc and must not be implemented in the . cpp file.
There is QObject::isSignalConnected()
for exactly that usecase - avoiding unnecessary work to prepare a signal emission when no one is listening. Its API docs even come with a nice example.
Example:
static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
if (isSignalConnected(valueChangedSignal)) {
QByteArray data;
data = get_the_value(); // expensive operation
emit valueChanged(data);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With