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. connect() new style syntax return result
Forum Updated to NodeBB v4.3 + New Features

connect() new style syntax return result

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 7 Posters 2.9k Views 6 Watching
  • 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.
  • J Offline
    J Offline
    JonB
    wrote on 14 Aug 2020, 09:55 last edited by
    #1

    In answering https://forum.qt.io/topic/118027/how-to-connect-menu-action-with-existing-slot-programatically/5, I came across https://doc.qt.io/qt-5/qobject.html#connect::

    The function returns a QMetaObject::Connection that represents a handle to a connection if it successfully connects the signal to the slot. The connection handle will be invalid if it cannot create the connection, for example, if QObject is unable to verify the existence of either signal or method, or if their signatures aren't compatible. You can check if the handle is valid by casting it to a bool.

    I have yet to see any code which bothers to check the result, Certainly if people insist on using the old-style SIGNAL/SLOT() macros they should be doing this, as witness that post.

    Question: if I stick to new-style compile-time connect(), are there any/many circumstances when it could still fail? In this case how safe am I assuming the connect() will have succeeded without bothering to check all my connect() run-time return results?

    J P 2 Replies Last reply 14 Aug 2020, 10:01
    0
    • J JonB
      14 Aug 2020, 09:55

      In answering https://forum.qt.io/topic/118027/how-to-connect-menu-action-with-existing-slot-programatically/5, I came across https://doc.qt.io/qt-5/qobject.html#connect::

      The function returns a QMetaObject::Connection that represents a handle to a connection if it successfully connects the signal to the slot. The connection handle will be invalid if it cannot create the connection, for example, if QObject is unable to verify the existence of either signal or method, or if their signatures aren't compatible. You can check if the handle is valid by casting it to a bool.

      I have yet to see any code which bothers to check the result, Certainly if people insist on using the old-style SIGNAL/SLOT() macros they should be doing this, as witness that post.

      Question: if I stick to new-style compile-time connect(), are there any/many circumstances when it could still fail? In this case how safe am I assuming the connect() will have succeeded without bothering to check all my connect() run-time return results?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 14 Aug 2020, 10:01 last edited by
      #2

      @JonB
      well, take this example:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QLineEdit e;
          QLabel *l;
          QMetaObject::Connection c = QObject::connect(&e, &QLineEdit::textChanged, l, &QLabel::setText);
      
          qDebug() << c;
      
          return a.exec();
      }
      

      it compiles fine, but connection fails, because the QLabel does technically not exist during the connect call


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      J 1 Reply Last reply 14 Aug 2020, 10:12
      4
      • J J.Hilk
        14 Aug 2020, 10:01

        @JonB
        well, take this example:

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QLineEdit e;
            QLabel *l;
            QMetaObject::Connection c = QObject::connect(&e, &QLineEdit::textChanged, l, &QLabel::setText);
        
            qDebug() << c;
        
            return a.exec();
        }
        

        it compiles fine, but connection fails, because the QLabel does technically not exist during the connect call

        J Offline
        J Offline
        JonB
        wrote on 14 Aug 2020, 10:12 last edited by
        #3

        @J-Hilk
        Yep.

        In that case, I want to see all the code you have ever written even with the new style syntax where, as a good citizen, you run-time check the return result of every connect() you ever perform? And a promise that you never fail to do so?

        J 1 Reply Last reply 14 Aug 2020, 10:15
        1
        • J JonB
          14 Aug 2020, 10:12

          @J-Hilk
          Yep.

          In that case, I want to see all the code you have ever written even with the new style syntax where, as a good citizen, you run-time check the return result of every connect() you ever perform? And a promise that you never fail to do so?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 14 Aug 2020, 10:15 last edited by
          #4

          @JonB :P

          sure, no it's the only case that I can think of, where the connect "could fail"
          you asked for any case 😉


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply 14 Aug 2020, 10:29
          0
          • J J.Hilk
            14 Aug 2020, 10:15

            @JonB :P

            sure, no it's the only case that I can think of, where the connect "could fail"
            you asked for any case 😉

            J Offline
            J Offline
            JonB
            wrote on 14 Aug 2020, 10:29 last edited by
            #5

            @J-Hilk
            I asked an intentional question: I know you to be a good citizen, so do you check all your new-style connect()s at runtime? I should like to know....

            J K S 3 Replies Last reply 14 Aug 2020, 10:30
            0
            • J JonB
              14 Aug 2020, 10:29

              @J-Hilk
              I asked an intentional question: I know you to be a good citizen, so do you check all your new-style connect()s at runtime? I should like to know....

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 14 Aug 2020, 10:30 last edited by
              #6

              @JonB said in connect() new style syntax return result:

              so do you check all your new-style connect()s at runtime?

              tbth
              not a single one😅


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              J 1 Reply Last reply 14 Aug 2020, 10:44
              1
              • J J.Hilk
                14 Aug 2020, 10:30

                @JonB said in connect() new style syntax return result:

                so do you check all your new-style connect()s at runtime?

                tbth
                not a single one😅

                J Offline
                J Offline
                JonB
                wrote on 14 Aug 2020, 10:44 last edited by
                #7

                @J-Hilk
                Exactly ;-)

                Though from now on I shall be strongly recommending it for those who insist on old-style, run-time behaviour.

                Other than your (correct) example, can you think of any others, assuming the signaller/slotter are, say, correct? I'm trying to understand whether there is anything during the (new style) connect() which could still fail, even if my code looks right?

                J 1 Reply Last reply 14 Aug 2020, 10:47
                0
                • J JonB
                  14 Aug 2020, 10:44

                  @J-Hilk
                  Exactly ;-)

                  Though from now on I shall be strongly recommending it for those who insist on old-style, run-time behaviour.

                  Other than your (correct) example, can you think of any others, assuming the signaller/slotter are, say, correct? I'm trying to understand whether there is anything during the (new style) connect() which could still fail, even if my code looks right?

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 14 Aug 2020, 10:47 last edited by
                  #8

                  @JonB I'm not sure in the case of custom argument types (enums, classes etc) when you send them via Qt::QueuedConnection I think those will also fail silently, If you failed to correctly register the types with the meta system.

                  If you forget a copy constructor for your custom class you should get a completive error


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  J 1 Reply Last reply 14 Aug 2020, 10:51
                  1
                  • J J.Hilk
                    14 Aug 2020, 10:47

                    @JonB I'm not sure in the case of custom argument types (enums, classes etc) when you send them via Qt::QueuedConnection I think those will also fail silently, If you failed to correctly register the types with the meta system.

                    If you forget a copy constructor for your custom class you should get a completive error

                    J Offline
                    J Offline
                    JonB
                    wrote on 14 Aug 2020, 10:51 last edited by JonB
                    #9

                    @J-Hilk
                    Thanks, I'll keep this open for a day or so, in case it inspires interesting comments when the other experts finally get out of bed :) I can't believe they would ever fail to check a return result which might fail......

                    I now feel "unclean" with all my existing connect() code :( I hadn't even looked at the return result, all the examples out there and what is posted on this forum never bother so I hadn't investigated....

                    J 1 Reply Last reply 14 Aug 2020, 10:55
                    1
                    • J JonB
                      14 Aug 2020, 10:51

                      @J-Hilk
                      Thanks, I'll keep this open for a day or so, in case it inspires interesting comments when the other experts finally get out of bed :) I can't believe they would ever fail to check a return result which might fail......

                      I now feel "unclean" with all my existing connect() code :( I hadn't even looked at the return result, all the examples out there and what is posted on this forum never bother so I hadn't investigated....

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 14 Aug 2020, 10:55 last edited by
                      #10

                      @JonB aren't you glad that c++17 introduced [[nodiscard]]

                      😉 I'm, but I doubt QObject::connect will ever geht that attribute, but could be fun for a April fools joke, to submit such a patch 🤣


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      J 1 Reply Last reply 14 Aug 2020, 11:01
                      1
                      • J JonB
                        14 Aug 2020, 10:29

                        @J-Hilk
                        I asked an intentional question: I know you to be a good citizen, so do you check all your new-style connect()s at runtime? I should like to know....

                        K Offline
                        K Offline
                        KroMignon
                        wrote on 14 Aug 2020, 11:00 last edited by
                        #11

                        @JonB said in connect() new style syntax return result:

                        I know you to be a good citizen, so do you check all your new-style connect()s at runtime? I should like to know....

                        If you are using new-style connect(), the return value is not that relevant.
                        The only interesting use case I see, is to use it to disconnect a specific slots/lambda function.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        J 1 Reply Last reply 14 Aug 2020, 11:05
                        1
                        • J J.Hilk
                          14 Aug 2020, 10:55

                          @JonB aren't you glad that c++17 introduced [[nodiscard]]

                          😉 I'm, but I doubt QObject::connect will ever geht that attribute, but could be fun for a April fools joke, to submit such a patch 🤣

                          J Offline
                          J Offline
                          JonB
                          wrote on 14 Aug 2020, 11:01 last edited by
                          #12

                          @J-Hilk
                          Excellent! I'm sure all existing code would welcome this attribute being auto-added ;-)

                          1 Reply Last reply
                          0
                          • K KroMignon
                            14 Aug 2020, 11:00

                            @JonB said in connect() new style syntax return result:

                            I know you to be a good citizen, so do you check all your new-style connect()s at runtime? I should like to know....

                            If you are using new-style connect(), the return value is not that relevant.
                            The only interesting use case I see, is to use it to disconnect a specific slots/lambda function.

                            J Offline
                            J Offline
                            JonB
                            wrote on 14 Aug 2020, 11:05 last edited by
                            #13

                            @KroMignon
                            Indeed! Nonetheless, @J-Hilk's example of an uninitialized variable might have been picked up. Though I suppose you will say we can never guard against, say, bad variables in this way....

                            Being an old C programmer, perhaps I should define connect() as a macro with a Q_ASSERT wrapper or similar on the return result...? ;-) [<-- Note the "wink", I am aware what you will think of that :) ]

                            K 1 Reply Last reply 14 Aug 2020, 11:20
                            0
                            • J JonB
                              14 Aug 2020, 11:05

                              @KroMignon
                              Indeed! Nonetheless, @J-Hilk's example of an uninitialized variable might have been picked up. Though I suppose you will say we can never guard against, say, bad variables in this way....

                              Being an old C programmer, perhaps I should define connect() as a macro with a Q_ASSERT wrapper or similar on the return result...? ;-) [<-- Note the "wink", I am aware what you will think of that :) ]

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 14 Aug 2020, 11:20 last edited by
                              #14

                              @JonB said in connect() new style syntax return result:

                              Being an old C programmer, perhaps I should define connect() as a macro with a Q_ASSERT wrapper or similar on the return result...? ;-) [<-- Note the "wink", I am aware what you will think of that :) ]

                              I am also a far older (embedded) C programmer as a C++ programmer ;-)
                              But I am not sure this is the best idea for checking connect() return value...

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              J 1 Reply Last reply 14 Aug 2020, 12:26
                              0
                              • K KroMignon
                                14 Aug 2020, 11:20

                                @JonB said in connect() new style syntax return result:

                                Being an old C programmer, perhaps I should define connect() as a macro with a Q_ASSERT wrapper or similar on the return result...? ;-) [<-- Note the "wink", I am aware what you will think of that :) ]

                                I am also a far older (embedded) C programmer as a C++ programmer ;-)
                                But I am not sure this is the best idea for checking connect() return value...

                                J Offline
                                J Offline
                                JonB
                                wrote on 14 Aug 2020, 12:26 last edited by
                                #15

                                @KroMignon said in connect() new style syntax return result:

                                But I am not sure this is the best idea for checking connect() return value...

                                It is when you don't want to change any lines of code, other than adding one #define :)

                                1 Reply Last reply
                                0
                                • J JonB
                                  14 Aug 2020, 09:55

                                  In answering https://forum.qt.io/topic/118027/how-to-connect-menu-action-with-existing-slot-programatically/5, I came across https://doc.qt.io/qt-5/qobject.html#connect::

                                  The function returns a QMetaObject::Connection that represents a handle to a connection if it successfully connects the signal to the slot. The connection handle will be invalid if it cannot create the connection, for example, if QObject is unable to verify the existence of either signal or method, or if their signatures aren't compatible. You can check if the handle is valid by casting it to a bool.

                                  I have yet to see any code which bothers to check the result, Certainly if people insist on using the old-style SIGNAL/SLOT() macros they should be doing this, as witness that post.

                                  Question: if I stick to new-style compile-time connect(), are there any/many circumstances when it could still fail? In this case how safe am I assuming the connect() will have succeeded without bothering to check all my connect() run-time return results?

                                  P Offline
                                  P Offline
                                  Pablo J. Rogina
                                  wrote on 14 Aug 2020, 13:14 last edited by
                                  #16

                                  @JonB said in connect() new style syntax return result:

                                  Certainly if people insist on using the old-style SIGNAL/SLOT() macros

                                  One of the things I see regarding old-style approach, is the lots and lots of examples still around using it. So for newcomers to Qt, having that available to start with is somehow misleading...

                                  Upvote the answer(s) that helped you solve the issue
                                  Use "Topic Tools" button to mark your post as Solved
                                  Add screenshots via postimage.org
                                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                  J J 2 Replies Last reply 14 Aug 2020, 13:30
                                  1
                                  • P Pablo J. Rogina
                                    14 Aug 2020, 13:14

                                    @JonB said in connect() new style syntax return result:

                                    Certainly if people insist on using the old-style SIGNAL/SLOT() macros

                                    One of the things I see regarding old-style approach, is the lots and lots of examples still around using it. So for newcomers to Qt, having that available to start with is somehow misleading...

                                    J Offline
                                    J Offline
                                    J.Hilk
                                    Moderators
                                    wrote on 14 Aug 2020, 13:30 last edited by
                                    #17

                                    @Pablo-J-Rogina
                                    there's almost no situation where the qt5 syntax wouldn't work as well, so maybe marking it as deprecated could be an option. 🤷‍♂️


                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    P 1 Reply Last reply 14 Aug 2020, 14:15
                                    0
                                    • P Pablo J. Rogina
                                      14 Aug 2020, 13:14

                                      @JonB said in connect() new style syntax return result:

                                      Certainly if people insist on using the old-style SIGNAL/SLOT() macros

                                      One of the things I see regarding old-style approach, is the lots and lots of examples still around using it. So for newcomers to Qt, having that available to start with is somehow misleading...

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 14 Aug 2020, 13:30 last edited by
                                      #18

                                      @Pablo-J-Rogina
                                      Exactly. Hence I often evangelise with noobs to get changed over. I realize it's hard with all the examples being old-style, and somehow they find new style --- (and lambdas) --- trickier. But judging by some of the answers where their old-style connect is wrong but they don't know it is till runtime problems, I think it will help them to move to new because they get compile-time error and importantly they get auto-completion of only suitable methods to use.

                                      1 Reply Last reply
                                      0
                                      • J J.Hilk
                                        14 Aug 2020, 13:30

                                        @Pablo-J-Rogina
                                        there's almost no situation where the qt5 syntax wouldn't work as well, so maybe marking it as deprecated could be an option. 🤷‍♂️

                                        P Offline
                                        P Offline
                                        Pablo J. Rogina
                                        wrote on 14 Aug 2020, 14:15 last edited by
                                        #19

                                        @J-Hilk said in connect() new style syntax return result:

                                        there's almost no situation where the qt5 syntax wouldn't work as well,

                                        Please don't misunderstand me. I'm in full favor of the new syntax, the compile time checking is a great advantage.

                                        marking it as deprecated could be an option

                                        That would be great! Do we need to create a feature request?

                                        Upvote the answer(s) that helped you solve the issue
                                        Use "Topic Tools" button to mark your post as Solved
                                        Add screenshots via postimage.org
                                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                        K 1 Reply Last reply 14 Aug 2020, 14:32
                                        0
                                        • P Pablo J. Rogina
                                          14 Aug 2020, 14:15

                                          @J-Hilk said in connect() new style syntax return result:

                                          there's almost no situation where the qt5 syntax wouldn't work as well,

                                          Please don't misunderstand me. I'm in full favor of the new syntax, the compile time checking is a great advantage.

                                          marking it as deprecated could be an option

                                          That would be great! Do we need to create a feature request?

                                          K Offline
                                          K Offline
                                          KroMignon
                                          wrote on 14 Aug 2020, 14:32 last edited by
                                          #20

                                          @Pablo-J-Rogina said in connect() new style syntax return result:

                                          That would be great! Do we need to create a feature request?

                                          I don't agree with this!
                                          There are some situation where using old syntax has advantages.
                                          For example, suppose you have some classes with implements QObjects which all have a slot void doWork(void), you could connect to this slot without having to know from which class the instance is based.
                                          That's not possible with new syntax, also slots overload handling is not so easy to use.
                                          I agree that overloading slots is not a good code practice.

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          1 Reply Last reply
                                          2

                                          1/23

                                          14 Aug 2020, 09:55

                                          • Login

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