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. can't connect overloaded method to a lambda function
Forum Updated to NodeBB v4.3 + New Features

can't connect overloaded method to a lambda function

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.8k Views 2 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.
  • duburlanD Offline
    duburlanD Offline
    duburlan
    wrote on last edited by
    #1

    I can't compile the following code

    class Foo : public QWidget
    {
        Q_OBJECT
    public:
        Foo()
        {
            QSpinBox *spinBox = new QSpinBox(this);
            connect(spinBox, &QSpinBox::valueChanged, [this](int val){/*do smth*/});
        }
    };
    

    The error is

    error: no matching function for call to 'Foo::connect(QSpinBox*&, <unresolved overloaded function type>, Foo::Foo()::<lambda(int)>)'
             connect(spinBox, &QSpinBox::valueChanged, [this](int val){/*do smth*/});
                                                                                  ^
    

    I guess the problem is "unresolved overloaded function type", because actually we have two overloaded methods in class QSpinBox:

        void valueChanged(int);
        void valueChanged(const QString &);
    

    Any ideas how to help compiler to resolve the right method?

    My specs:
    GCC 4.9.2
    Qt 5.5.1

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      Try

       connect(spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [this]( int val){/*do smth*/});
      

      https://wiki.qt.io/New_Signal_Slot_Syntax

      ...because QSpinBox has two signals named valueChanged() with different arguments. Instead, the new code needs to be:
      connect(mySpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), mySlider, &QSlider::setValue);

      As you seem to think also.

      1 Reply Last reply
      1
      • duburlanD Offline
        duburlanD Offline
        duburlan
        wrote on last edited by
        #3

        Thanks, that works. Though, it doesn't look elegant...

        P.S. Such a syntax also works

        connect(spinBox, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this]( int val){/*do smth*/});
        
        mrjjM 1 Reply Last reply
        0
        • duburlanD duburlan

          Thanks, that works. Though, it doesn't look elegant...

          P.S. Such a syntax also works

          connect(spinBox, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this]( int val){/*do smth*/});
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @duburlan
          I agree.
          Its not pretty.

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #5

            If you don't mind writing more lines, you can make each line shorter:

            void (QSpinBox::*mySignal)(int) = &QSpinBox::valueChanged;
            
            connect(spinBox, mySignal, this, [this](int val) {
                /*do smth*/
            });
            

            See http://doc.qt.io/qt-5/signalsandslots-syntaxes.html

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

            1 Reply Last reply
            2
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              far more readable :)

              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