Determine if the slot is connected to the given signal
I'm curious to see if there is a way to determine that there is a connection to a given signal that I have defined in the class. This is basically a signal to process the data and I don't care what it's related to, but I would like to enable a health check so that I don't send data into a void where it will never be seen. I have checked Determine signals connected to a given slot in Qt , but this is kind of the opposite problem.
+3
source to share
1 answer
QObject::isSignalConnected()
QObject::isSignalConnected()
exactly for usecase - avoids unnecessary work to prepare the signal when no one is listening. Its API docs even come with a good example.
Example:
static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
if (isSignalConnected(valueChangedSignal)) {
QByteArray data;
data = get_the_value(); // expensive operation
emit valueChanged(data);
}
+6
source to share