Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSignalSpy::isValid() - Deeper explanation
QtWS25 Last Chance

QSignalSpy::isValid() - Deeper explanation

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsignalspyqverifyisvalid
9 Posts 3 Posters 2.9k Views
  • 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 Offline
    A Offline
    Aram
    wrote on 14 Dec 2015, 07:39 last edited by Aram
    #1

    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
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 Dec 2015, 07:42 last edited by
      #2

      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 14 Dec 2015, 07:54
      0
      • A Offline
        A Offline
        Aram
        wrote on 14 Dec 2015, 07:44 last edited by
        #3

        well it just passes the test case...

        K 1 Reply Last reply 14 Dec 2015, 07:53
        0
        • A Aram
          14 Dec 2015, 07:44

          well it just passes the test case...

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 14 Dec 2015, 07:53 last edited by kshegunov
          #4

          @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 14 Dec 2015, 08:00
          1
          • J jsulm
            14 Dec 2015, 07:42

            What happens if you do:

            QCheckBox *box = ...;
            QSignalSpy spy(box, SIGNAL(doesNotExist(bool)));
            QVERIFY(spy.isValid());
            
            A Offline
            A Offline
            Aram
            wrote on 14 Dec 2015, 07:54 last edited by
            #5

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

            J 1 Reply Last reply 14 Dec 2015, 08:03
            0
            • K kshegunov
              14 Dec 2015, 07:53

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

              A Offline
              A Offline
              Aram
              wrote on 14 Dec 2015, 08:00 last edited by
              #6

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

              K 1 Reply Last reply 14 Dec 2015, 08:02
              0
              • A Aram
                14 Dec 2015, 08:00

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

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 14 Dec 2015, 08:02 last edited by
                #7

                @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
                1
                • A Aram
                  14 Dec 2015, 07:54

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 14 Dec 2015, 08:03 last edited by
                  #8

                  @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 14 Dec 2015, 08:05
                  1
                  • J jsulm
                    14 Dec 2015, 08:03

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

                    A Offline
                    A Offline
                    Aram
                    wrote on 14 Dec 2015, 08:05 last edited by Aram
                    #9

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

                    3/9

                    14 Dec 2015, 07:44

                    6 unread
                    • Login

                    • Login or register to search.
                    3 out of 9
                    • First post
                      3/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved