Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QSignalSpy::isValid() - Deeper explanation

    General and Desktop
    qsignalspy qverify isvalid
    3
    9
    2186
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      Aram last edited by Aram

      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

      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        What happens if you do:

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply Reply Quote 0
        • A
          Aram last edited by

          well it just passes the test case...

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @Aram last edited by kshegunov

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

            Read and abide by the Qt Code of Conduct

            A 1 Reply Last reply Reply Quote 1
            • A
              Aram @jsulm last edited by

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

              jsulm 1 Reply Last reply Reply Quote 0
              • A
                Aram @kshegunov last edited by

                @kshegunov I see now.. so your object can be deleted during the program run so you have to make sure to check this... thanks

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @Aram last edited by

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

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply Reply Quote 1
                  • jsulm
                    jsulm Lifetime Qt Champion @Aram last edited by

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    A 1 Reply Last reply Reply Quote 1
                    • A
                      Aram @jsulm 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...;)

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post