Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect doesn't work with lambda function
Qt 6.12 LTS 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 5.0k 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.
  • A Aaron Kim
    MenuItem::MenuItem(QMainWindow *mainWindow, const QString &text, const int &num){
        this->setText(text);
        this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        this->setMaximumHeight(40);
        /*
        QSignalMapper* signalMapper = new QSignalMapper(this);
        connect(this, SIGNAL(clicked()), signalMapper, SLOT(map()));
        signalMapper->setMapping(this, num);
        connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeContent(int)));
        */
        auto lambda = [=](){
            static_cast<MainWindow*>(mainWindow)->changeContent(num);
        };
        connect(this, SIGNAL(clicked()), mainWindow, lambda);
    }
    

    To pass a parameter to the slot, I tried using QSignalMapper but it seems that it has been deprecated. So, I tried lambda function but an error occurs.

    C:\Users\Coder\Desktop\qt\Calculator\template\sidemenu\menuItem.cpp:16: error: no matching function for call to 'MenuItem::connect(MenuItem*, const char*, QMainWindow*&, MenuItem::MenuItem(QMainWindow*, const QString&, const int&)::<lambda()>&)'
         connect(this, SIGNAL(clicked()), mainWindow, lambda);
    

    Does it have something to do with the lambda function itself?

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

    @Aaron-Kim
    whats the type of MenuItem?
    If it doesn't derive from QObject, you need to call QObject::connect(...) instead

    --- 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
    3
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      You can't mix old and new-style connect syntax ... --> connect(this, &MenuItem:clicked, ...)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • A Aaron Kim
        MenuItem::MenuItem(QMainWindow *mainWindow, const QString &text, const int &num){
            this->setText(text);
            this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            this->setMaximumHeight(40);
            /*
            QSignalMapper* signalMapper = new QSignalMapper(this);
            connect(this, SIGNAL(clicked()), signalMapper, SLOT(map()));
            signalMapper->setMapping(this, num);
            connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeContent(int)));
            */
            auto lambda = [=](){
                static_cast<MainWindow*>(mainWindow)->changeContent(num);
            };
            connect(this, SIGNAL(clicked()), mainWindow, lambda);
        }
        

        To pass a parameter to the slot, I tried using QSignalMapper but it seems that it has been deprecated. So, I tried lambda function but an error occurs.

        C:\Users\Coder\Desktop\qt\Calculator\template\sidemenu\menuItem.cpp:16: error: no matching function for call to 'MenuItem::connect(MenuItem*, const char*, QMainWindow*&, MenuItem::MenuItem(QMainWindow*, const QString&, const int&)::<lambda()>&)'
             connect(this, SIGNAL(clicked()), mainWindow, lambda);
        

        Does it have something to do with the lambda function itself?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #4

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

        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 SGaistS 2 Replies Last reply
        2
        • 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 Offline
              J.HilkJ Offline
              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 Offline
                              J.HilkJ Offline
                              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
                              • Unsolved