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. Adding Lambda Expression as Slot
Qt 6.11 is out! See what's new in the release blog

Adding Lambda Expression as Slot

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 6 Posters 4.6k 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.
  • D Offline
    D Offline
    Domididongo
    wrote on last edited by
    #1

    I want to create a Combobox and add to every combo box I add a function.

    connect(comboBox, SIGNAL(activated(QString)) ,this,  [](QString text){
            cout << text.toUtf8().constData() << endl;
        });
    

    But I always get as result : No matching member function for call to 'connect'

    jsulmJ raven-worxR 2 Replies Last reply
    0
    • D Domididongo

      I want to create a Combobox and add to every combo box I add a function.

      connect(comboBox, SIGNAL(activated(QString)) ,this,  [](QString text){
              cout << text.toUtf8().constData() << endl;
          });
      

      But I always get as result : No matching member function for call to 'connect'

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Domididongo Remove this:

      connect(comboBox, SIGNAL(activated(QString)) ,[](QString text){
              cout << text.toUtf8().constData() << endl;
          });
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Domididongo

        I want to create a Combobox and add to every combo box I add a function.

        connect(comboBox, SIGNAL(activated(QString)) ,this,  [](QString text){
                cout << text.toUtf8().constData() << endl;
            });
        

        But I always get as result : No matching member function for call to 'connect'

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

        @Domididongo said in Adding Lambda Expression as Slot:

        But I always get as result : No matching member function for call to 'connect'

        connect(comboBox, QOverload<QString>::of(&QComboBox::activated) , this,  [](const QString & text) {
                cout << text.toUtf8().constData() << endl;
            });
        

        --- 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
        • sneubertS Offline
          sneubertS Offline
          sneubert
          wrote on last edited by
          #4

          the new syntax needs the address of a class member:

          connect(comboBox, &QComboBox::activated ,this,  [](QString text){
                  cout << text.toUtf8().constData() << endl;
              });
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Because you are trying to use an overload that doesn't exists:

            connect(comboBox, qOverload<QString>(&QComboBox::activated), this, [](const QString &text){
                    cout << text.toUtf8().constData() << endl;
                });
            

            qOverload requires C++14.

            You don't even need the this parameter.

            For more information, see here

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

            raven-worxR 1 Reply Last reply
            4
            • SGaistS SGaist

              Hi,

              Because you are trying to use an overload that doesn't exists:

              connect(comboBox, qOverload<QString>(&QComboBox::activated), this, [](const QString &text){
                      cout << text.toUtf8().constData() << endl;
                  });
              

              qOverload requires C++14.

              You don't even need the this parameter.

              For more information, see here

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

              @sneubert said in Adding Lambda Expression as Slot:

              the new syntax needs the address of a class member:
              connect(comboBox, &QComboBox::activated ,this, [](QString text){
              cout << text.toUtf8().constData() << endl;
              });

              won't compile, since there are more than one activated() methods with different signatures. Thus the compiler won't know which one to link to.

              @SGaist said in Adding Lambda Expression as Slot:

              qOverload requires C++14.

              Just a note:
              the function qOverload<>() requires C++14
              the helper class QOverload<>::of() also works since C++11

              --- 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
              4
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                I'll be that guy. I apologise in advance.

                This is literally spelled out for you in the docs. you just need to copy-paste that code into your code. I can't think of a better way the docs could make this clear

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                6
                • sneubertS Offline
                  sneubertS Offline
                  sneubert
                  wrote on last edited by
                  #8

                  sorry I did not thought there´s an overload

                  1 Reply Last reply
                  0

                  • Login

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