Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSignalSpy::isValid() - Deeper explanation
Aram last edited by Aram
I was studying about QSignalSpy and had a look to its public function isValid() . And I was confused... Documentation says only that it "returns true if the signal spy listens to a valid signal, otherwise false." But this makes little sense to me as I don't know what does "VALID SIGNAL" mean... What is "NOT valid signal" then?
Let consider an example where I do QVERIFY
QCheckBox *box = ...; QSignalSpy spy(box, SIGNAL(clicked(bool))); QVERIFY(spy.isValid());
When will QVERIFY fail? Could you please bring more examples of and uses for isValid() ? And if you share your understanding of this so I (WE) can understand this deeper then it will be much appreciated!
What happens if you do:
QCheckBox *box = ...; QSignalSpy spy(box, SIGNAL(doesNotExist(bool))); QVERIFY(spy.isValid());
well it just passes the test case...
If QSignalSpy is not able to listen for a valid signal (for example, because object is null or signal does not denote a valid signal of object), an explanatory warning message will be output using qWarning() and subsequent calls to isValid() will return false.
I think this is quite clear. The
SIGNALmacro doesn't perform any compile time checks, so any function name (or any string actually) may look as a valid signal. The meta object system on the other hand knows (but in runtime) what signals (prototypes) are valid, and everything that it doesn't know about is not a valid signal. :)
@jsulm sorry didn't notice you changed the signals name to "doesNotExist(bool)"... well it fails then but this is very obvious case where you don't need to verify anything at all. its even can fail to compile...
@kshegunov I see now.. so your object can be deleted during the program run so you have to make sure to check this... thanks
If the object is deleted all of its connections are dropped. The point is that connections are made at runtime, not while compiling. Additionally the
SIGNALmacro just converts its argument to the appropriate string, nothing more. The macro itself cannot provide any compile-time error checking.
@Aram No, it will not fail to compile, it will only print a warning at runtime saying that there is no such signal.
Aram last edited by Aram
@jsulm yes, you are right... actually i meant "warning" in my mind when i typed fail... haha sorry (didn't want to edit my message...;)