Qt Forum

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

    Solved connect() new style syntax return result

    General and Desktop
    7
    23
    610
    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.
    • J.Hilk
      J.Hilk Moderators @JonB last edited by

      @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

      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

      JonB 1 Reply Last reply Reply Quote 1
      • JonB
        JonB @J.Hilk last edited by JonB

        @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.Hilk 1 Reply Last reply Reply Quote 1
        • J.Hilk
          J.Hilk Moderators @JonB last edited by

          @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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          JonB 1 Reply Last reply Reply Quote 1
          • KroMignon
            KroMignon @JonB last edited by

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

            JonB 1 Reply Last reply Reply Quote 1
            • JonB
              JonB @J.Hilk last edited by

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

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB @KroMignon last edited by

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

                KroMignon 1 Reply Last reply Reply Quote 0
                • KroMignon
                  KroMignon @JonB last edited by

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

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @KroMignon last edited by

                    @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 Reply Quote 0
                    • Pablo J. Rogina
                      Pablo J. Rogina @JonB last edited by

                      @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.Hilk JonB 2 Replies Last reply Reply Quote 1
                      • J.Hilk
                        J.Hilk Moderators @Pablo J. Rogina last edited by

                        @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

                        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                        Pablo J. Rogina 1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB @Pablo J. Rogina last edited by

                          @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 Reply Quote 0
                          • Pablo J. Rogina
                            Pablo J. Rogina @J.Hilk last edited by

                            @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

                            KroMignon 1 Reply Last reply Reply Quote 0
                            • KroMignon
                              KroMignon @Pablo J. Rogina last edited by

                              @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 Reply Quote 2
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                And IIRC, the "old" system is also what allows for things like QML to work its magic.

                                The porting of the example is a good idea. The documentation team would be happy to get some help with that so you are more than welcome to submit patches. I think there are tasks related to that on the bug tracker to help organise.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                JKSH 1 Reply Last reply Reply Quote 1
                                • S
                                  SimonSchroeder @JonB last edited by

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

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

                                  I have never written any checks and never seen any checks in any source code. In normal circumstances connects should be well structured and not occur everywhere in your source code. Connects for general QActions (menus, toolbars, etc.) we connect once at startup. When dialogs are constructed, we connect in the constructor (or the functions that creates a small dialog on demand). Rarely don't we have the object we'd like to connect. Then, there would not be any failures in connects when they succeded compiling in the new syntax. We still have a lot of old connects where it compiles even if the signal or slot name is wrong. However, Qt writes it out to the command line and this is where we catch these errors. So, in all our scenarios either the connect always succeeds or always fails. It does not depend on any runtime behaviour. This means, once a connect is known to work, we can forget about it.

                                  1 Reply Last reply Reply Quote 0
                                  • JKSH
                                    JKSH Moderators @SGaist last edited by JKSH

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

                                    And IIRC, the "old" system is also what allows for things like QML to work its magic.

                                    Correct. The old syntax lets us make connections between C++ code and QML code.

                                    It also supports dynamic meta objects which the new syntax can't support: https://www.qtdeveloperdays.com/sites/default/files/QtDevDays2014US-DIY-moc.pdf (useful for language bindings)

                                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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