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 doesn't work with lambda function
Qt 6.11 is out! See what's new in the release blog

Connect doesn't work with lambda function

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 7 Posters 4.2k Views 3 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.HilkJ J.Hilk

    @Aaron-Kim said in Connect doesn't work with lambda function:

    auto lambda = ={
    static_cast<MainWindow*>(mainWindow)->changeContent(num);
    };
    connect(this, SIGNAL(clicked()), mainWindow, lambda);

    dont mix Qt4 and Qt5 Signal/Slot syntax, also, Lambda functions only work with the new syntax.

        connect(this, &MenuItem::clicked, mainWindow,  [=](){
            static_cast<MainWindow*>(mainWindow)->changeContent(num);
        });
    
    A Offline
    A Offline
    Aaron Kim
    wrote on last edited by
    #5

    @J.Hilk Thanks! I didn't know that SIGNAL() is an old style.
    Should I change all the SINGAL() syntaxes to the new one?

    aha_1980A J.HilkJ mrjjM 3 Replies Last reply
    0
    • A Aaron Kim

      @J.Hilk Thanks! I didn't know that SIGNAL() is an old style.
      Should I change all the SINGAL() syntaxes to the new one?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Aaron-Kim said in [Solved] Connect doesn't work with lambda function:

      Should I change all the SINGAL() syntaxes to the new one?

      It is recommended to do so, indeed.

      One note: to solve the forum topics as SOLVED, please use the button topic tools below your first post, do not write [Solved] yourself.

      Thanks!

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • A Aaron Kim

        @J.Hilk Thanks! I didn't know that SIGNAL() is an old style.
        Should I change all the SINGAL() syntaxes to the new one?

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #7

        @Aaron-Kim said in [Solved] Connect doesn't work with lambda function:

        Should I change all the SINGAL() syntaxes to the new one?

        Well, thats up to you.

        Take a look at this wiki article and decide for yourself how to proceed further :)
        https://wiki.qt.io/New_Signal_Slot_Syntax


        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.

        A 1 Reply Last reply
        2
        • A Aaron Kim

          @J.Hilk Thanks! I didn't know that SIGNAL() is an old style.
          Should I change all the SINGAL() syntaxes to the new one?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #8

          Hi
          Just as a note.
          The new syntax will catch error at compile time and hence
          is preferred over the SIGNAL/SLOT ones as they will compile even
          the connect wont work.
          Also, there is a slight performance gain runtime with the new syntax.
          The downside is super ugly syntax with overloaded functions so make sure to
          check out qOverload

          1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @Aaron-Kim said in [Solved] Connect doesn't work with lambda function:

            Should I change all the SINGAL() syntaxes to the new one?

            Well, thats up to you.

            Take a look at this wiki article and decide for yourself how to proceed further :)
            https://wiki.qt.io/New_Signal_Slot_Syntax

            A Offline
            A Offline
            Aaron Kim
            wrote on last edited by
            #9

            @J.Hilk One more thing, Is there any reason for me to use &QPushButton::clicked, rather than just QPushButton::clicked?
            As far as I know, the signs & and * have no effect on function pointers. And I checked that QPushButton::clicked also works.

            raven-worxR 1 Reply Last reply
            0
            • A Aaron Kim

              @J.Hilk One more thing, Is there any reason for me to use &QPushButton::clicked, rather than just QPushButton::clicked?
              As far as I know, the signs & and * have no effect on function pointers. And I checked that QPushButton::clicked also works.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #10

              @Aaron-Kim said in Connect doesn't work with lambda function:

              As far as I know, the signs & and * have no effect on function pointers. And I checked that QPushButton::clicked also works.

              definitely thats not true.
              The method signature expects a function-pointer, hence the referencing with &

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              A 1 Reply Last reply
              2
              • raven-worxR raven-worx

                @Aaron-Kim said in Connect doesn't work with lambda function:

                As far as I know, the signs & and * have no effect on function pointers. And I checked that QPushButton::clicked also works.

                definitely thats not true.
                The method signature expects a function-pointer, hence the referencing with &

                A Offline
                A Offline
                Aaron Kim
                wrote on last edited by
                #11

                @raven-worx
                https://stackoverflow.com/questions/6893285/why-do-function-pointer-definitions-work-with-any-number-of-ampersands-or-as
                But this articles says that a function can be implicitly converted to a function pointer. Does the implementation of connect() require explicit function-pointer syntax with an ampersand?

                raven-worxR mrjjM 2 Replies Last reply
                0
                • A Aaron Kim

                  @raven-worx
                  https://stackoverflow.com/questions/6893285/why-do-function-pointer-definitions-work-with-any-number-of-ampersands-or-as
                  But this articles says that a function can be implicitly converted to a function pointer. Does the implementation of connect() require explicit function-pointer syntax with an ampersand?

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #12

                  @Aaron-Kim
                  my opinion: just stay on the safe side (by using the & function reference) and don't rely on any implicit compiler features. Also its clear what the code does on the first glance.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  1
                  • A Aaron Kim

                    @raven-worx
                    https://stackoverflow.com/questions/6893285/why-do-function-pointer-definitions-work-with-any-number-of-ampersands-or-as
                    But this articles says that a function can be implicitly converted to a function pointer. Does the implementation of connect() require explicit function-pointer syntax with an ampersand?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @Aaron-Kim
                    Hi
                    They talk about pointer to a (c) function but with the
                    connect we are talking pointer to a member function.
                    Also with no &
                    connect(ui->pushButton, QPushButton::released, this, { qDebug() << "test";} );
                    looks like a call to a static member function for compiler
                    so & is need to mean take address of this member.

                    1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @Aaron-Kim said in Connect doesn't work with lambda function:

                      auto lambda = ={
                      static_cast<MainWindow*>(mainWindow)->changeContent(num);
                      };
                      connect(this, SIGNAL(clicked()), mainWindow, lambda);

                      dont mix Qt4 and Qt5 Signal/Slot syntax, also, Lambda functions only work with the new syntax.

                          connect(this, &MenuItem::clicked, mainWindow,  [=](){
                              static_cast<MainWindow*>(mainWindow)->changeContent(num);
                          });
                      
                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Hi,

                      @J.Hilk said in Connect doesn't work with lambda function:

                      static_cast<MainWindow*>(mainWindow)->changeContent(num);

                      Also you should rather use qobject_cast when dealing with QObject derived classes.

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

                      J.HilkJ 1 Reply Last reply
                      2
                      • SGaistS SGaist

                        Hi,

                        @J.Hilk said in Connect doesn't work with lambda function:

                        static_cast<MainWindow*>(mainWindow)->changeContent(num);

                        Also you should rather use qobject_cast when dealing with QObject derived classes.

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #15

                        @SGaist
                        very true!
                        My bad though, was copy and pasting from the op 🙈


                        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.

                        1 Reply Last reply
                        1

                        • Login

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