Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Q: Qt - [c++] Correct CONNECT statements with lambda
Forum Updated to NodeBB v4.3 + New Features

Q: Qt - [c++] Correct CONNECT statements with lambda

Scheduled Pinned Locked Moved Solved Game Development
11 Posts 5 Posters 533 Views 1 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.
  • K Offline
    K Offline
    kevin_d
    wrote 19 days ago last edited by
    #1

    Hello!

    I am using a button with a signal and an action passed as lambda to the CONNECT callback.
    Like this:

    CONNECT(myClickButton &QPushButton::clicked, [=] { myWidget->onClick(); });
    

    Qt 6.8 claims, to pass a context object as third parameter.
    What is meant by that?
    I used passing "this" as third object (before the lambda) and varied the lambda catch list. But still get this warning information.

    Which is the suitable call for this?

    best regards, kevin_d

    S 1 Reply Last reply 17 days ago
    0
    • A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote 19 days ago last edited by aha_1980
      #2

      Hi @kevin_d,

      Your statement does not look like valid C++ code, I guess you mean:

      connect(myClickButton, &QPushButton::clicked, [=] { myWidget->onClick(); });
      

      The context object is used to disconnect the connection between myClickButton and the lambda if anything goes out of scope. So often the third argument is this, but in your example it could also be myWidget, e.g.:

      connect(myClickButton, &QPushButton::clicked, myWidget, [=] { myWidget->onClick(); });
      

      but probably you don't even need a lambda here, as you could write:

      connect(myClickButton, &QPushButton::clicked, myWidget, &MyWidget::onClick);
      

      where MyWidget would be the class name of myWidget.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      4
      • J Offline
        J Offline
        JonB
        wrote 18 days ago last edited by JonB
        #3

        @kevin_d , @aha_1980
        Thank you, I have learned something new from this! My initial reaction was that

        [=] { myWidget->onClick(); }
        

        must be wrong because it does not have any parentheses. I always write

        [=] () { myWidget->onClick(); }
        

        But I looked it up and indeed the parameter list is optional, where I thought it was mandatory.

        At times it baffles me why C++ makes some things so complicated. You cannot define, declare or call a function without the (). And a lambda is like an (anonymous) function definition/call. Why C++ goes and make this optional (when no parameters) is beyond me! I find it so much clearer when looking through code to have a lambda use even empty () so it looks like a function. Ho hum!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote 18 days ago last edited by aha_1980
          #4

          @JonB So I suggest you to get some popcorn and enjoy this old thread of mine: https://codereview.qt-project.org/c/qt-creator/qt-creator/+/249192

          Qt has to stay free or it will die.

          J 1 Reply Last reply 18 days ago
          1
          • A aha_1980
            18 days ago

            @JonB So I suggest you to get some popcorn and enjoy this old thread of mine: https://codereview.qt-project.org/c/qt-creator/qt-creator/+/249192

            J Offline
            J Offline
            JonB
            wrote 18 days ago last edited by
            #5

            @aha_1980
            I am glad to see the https://wiki.qt.io/Coding_Conventions#Lambdas you reference says

            Always write parentheses for the parameter list, even if the function does not take parameters

            But I think the codereview thread ended up on the side of the title, "Coding-Style: Leave out parentheses for empty lambda parameter list", is that right? Who "won"?

            If so: I guess I do respect that if no parentheses is the Qt internal style (is it?) then people submitting patches etc. should stick to it. The trouble is personally I cannot see any merit at all in omitting them when there are no parameters. Any more than C++ functions do not omit it. Personally I cannot agree with comment there

            I also prefer not writing empty () for lambdas. It doesn't add any value for readability.

            It 100% does add readability for me and seemingly the majority of others. (Not to mention the "exceptions" where you have to have them, like mutable.) And for "beginners", where lambda syntax is empirically tricky, it's a lot clearer how you can later add parameters. I cannot see any merit in the omission. That's the trouble with this, and "opinions", and the you-know-what holes --- which everybody has.

            S 1 Reply Last reply 17 days ago
            0
            • A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote 18 days ago last edited by
              #6

              Who "won"?

              I'd say nobody, because as you noticed, the wiki recommends the style with () and a lot of developers don't accept that. In Qt Creator, we have the majority of lambdas without braces, but still 500 with empty braces.

              So it seems to only depend on the author and the reviewer.

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • K kevin_d
                19 days ago

                Hello!

                I am using a button with a signal and an action passed as lambda to the CONNECT callback.
                Like this:

                CONNECT(myClickButton &QPushButton::clicked, [=] { myWidget->onClick(); });
                

                Qt 6.8 claims, to pass a context object as third parameter.
                What is meant by that?
                I used passing "this" as third object (before the lambda) and varied the lambda catch list. But still get this warning information.

                Which is the suitable call for this?

                best regards, kevin_d

                S Offline
                S Offline
                SimonSchroeder
                wrote 17 days ago last edited by
                #7

                @kevin_d said in Q: Qt - [c++] Correct CONNECT statements with lambda:

                Qt 6.8 claims, to pass a context object as third parameter.

                There is a hidden feature of connecting signals in Qt: If any of the objects ends their lifetime, the connections will be automatically disconnected. With the example of

                connect(myClickButton, &QPushButton::clicked, myWidget, &MyWidget::onClick);
                

                the connection will be disconnected if either myClickButton or myWidget gets deleted. This is a good thing because with a myClickButton there is no signal to send anymore and without myWidget there is no receiver for the signal anymore.

                In the case of a lambda instead of a slot for the connection you should think which object is required for the lambda to work. In your simple example this is myWidget and thus the context object should be myWidget. Sometimes there is no reasonable object for the context object and then I'll just put this.

                There is yet another reason for the context object: If you are using multiple thread the lambda will be executed inside the thread the context object belongs to. Sometimes you might want to choose qApp to make sure the lambda is executed inside the GUI thread.

                1 Reply Last reply
                1
                • J JonB
                  18 days ago

                  @aha_1980
                  I am glad to see the https://wiki.qt.io/Coding_Conventions#Lambdas you reference says

                  Always write parentheses for the parameter list, even if the function does not take parameters

                  But I think the codereview thread ended up on the side of the title, "Coding-Style: Leave out parentheses for empty lambda parameter list", is that right? Who "won"?

                  If so: I guess I do respect that if no parentheses is the Qt internal style (is it?) then people submitting patches etc. should stick to it. The trouble is personally I cannot see any merit at all in omitting them when there are no parameters. Any more than C++ functions do not omit it. Personally I cannot agree with comment there

                  I also prefer not writing empty () for lambdas. It doesn't add any value for readability.

                  It 100% does add readability for me and seemingly the majority of others. (Not to mention the "exceptions" where you have to have them, like mutable.) And for "beginners", where lambda syntax is empirically tricky, it's a lot clearer how you can later add parameters. I cannot see any merit in the omission. That's the trouble with this, and "opinions", and the you-know-what holes --- which everybody has.

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote 17 days ago last edited by
                  #8

                  @JonB said in Q: Qt - [c++] Correct CONNECT statements with lambda:

                  But I think the codereview thread ended up on the side of the title, "Coding-Style: Leave out parentheses for empty lambda parameter list", is that right? Who "won"?

                  Until recently, I would have claimed that you should write the parentheses. Why? Writing [] mutable {} used to be illegal until C++23 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2036r3.html). You had to write []() mutable {}. For consistency's sake it would make sense to write the parentheses up until C++20. Same with specifying the return type.

                  Still, I would argue that one of the defining features of a function is that it has a paramter list (even if it is empty). Lambdas are also functions. Consistency dictates that you write the parentheses. But, the C++ committee seems to think otherwise with not making them mandatory in even more cases with C++23.

                  1 Reply Last reply
                  0
                  • J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote 17 days ago last edited by
                    #9

                    Lambdas in c++ are horribly inconsistent. It's a shame that you can omit so much. IMHO everything should be written out, including return type!

                    And thats what I', doing in all my code base :P


                    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.

                    S 1 Reply Last reply 16 days ago
                    1
                    • J.HilkJ J.Hilk
                      17 days ago

                      Lambdas in c++ are horribly inconsistent. It's a shame that you can omit so much. IMHO everything should be written out, including return type!

                      And thats what I', doing in all my code base :P

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote 16 days ago last edited by
                      #10

                      @J.Hilk I believe the initial motivation for lambdas was to be used in algorithms, e.g.:

                      std::accumulate(vec.begin(), vec.end(), 0.0, [](double a, double b) { return a+b; });
                      

                      In general, this was quite cumbersome to write with a free-standing function (obviously defined far away from its actual use) or a function object (overloading operator() if you needed to 'capture' a variable).

                      Back then, I resorted to using Boost.Lambda, which actually was a lot shorter:

                      std::accumulate(vec.begin(), vec.end(), 0.0, _1+_2);
                      

                      However, lambdas are not just tiny one-liner functions anymore, but people use them for other things as well. I understand the urge to have a small lambda in the case I mentioned above. Sometimes, I just use lambdas to have locally scoped "free-standing" functions and thus don't try to be as minimal as possible anyway.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kevin_d
                        wrote 14 days ago last edited by
                        #11

                        Well thanks for all your suggestions! Thread solved.

                        best regards

                        1 Reply Last reply
                        1
                        • K kevin_d has marked this topic as solved 14 days ago

                        1/11

                        17 May 2025, 17:58

                        • Login

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