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 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!
    Thanks


  • Moderators

    What happens if you do:

    QCheckBox *box = ...;
    QSignalSpy spy(box, SIGNAL(doesNotExist(bool)));
    QVERIFY(spy.isValid());
    


  • well it just passes the test case...


  • Qt Champions 2016

    @Aram

    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. :)



  • @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


  • Qt Champions 2016

    @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 the SIGNAL macro just converts its argument to the appropriate string, nothing more. The macro itself cannot provide any compile-time error checking.


  • Moderators

    @Aram No, it will not fail to compile, it will only print a warning at runtime saying that there is no such signal.



  • @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...;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.