QSignalSpy::isValid() - Deeper explanation
-
Hi all,
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 QVERIFYQCheckBox *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!
Thanks -
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
SIGNAL
macro 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. :) -
@kshegunov I see now.. so your object can be deleted during the program run so you have to make sure to check this... thanks
-
@Aram
If the object is deleted all of its connections are dropped. The point is that connections are made at runtime, not while compiling. Additionally theSIGNAL
macro 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.
9/9