1

I'm study C++ Qt. And now try to use lambda function with connect:

connect(ui->sbNormal, &QSpinBox::valueChanged, [=] (int x) {});

It output error:

error: no matching function for call to 'MainWindow::connect(QSpinBox*&, < unresolved overloaded function type>, MainWindow::MainWindow(QWidget*)::< lambda(int)>)'});

What am I do wrong? How to specify needed overloading?

Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • What is the signature of the function you are trying to call? I would guess that it takes a function pointer? – NathanOliver Mar 28 '16 at 19:47
  • `void valueChanged(int)` and `void valueChanged(const QString &)`. I need tocall first – Denis Sologub Mar 28 '16 at 19:48
  • I meant the `connect` function – NathanOliver Mar 28 '16 at 19:49
  • I don't know... it has a lot of overloads. But this variant works: `connect(sender, &Sender::valueChanged, [=](const QString &newValue) { receiver->updateValue("senderValue", newValue); } );` I got it from Qt docs. – Denis Sologub Mar 28 '16 at 19:51
  • I found how to cast function manually! :) – Denis Sologub Mar 28 '16 at 19:57
  • 2
    Possible duplicate of [Connecting overloaded signals and slots in Qt 5](http://stackoverflow.com/questions/16794695/connecting-overloaded-signals-and-slots-in-qt-5) – Toby Speight May 18 '16 at 14:57
  • Possible duplicate of [Qt connect function - signal disambiguation using lambdas](http://stackoverflow.com/questions/26062397/qt-connect-function-signal-disambiguation-using-lambdas) – mpromonet May 18 '16 at 16:50

1 Answers1

4

In case there are multiple overloads, you have to specify which one you want manually:

connect(ui->sbNormal, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
        [=] (int x) {});
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
dtech
  • 47,916
  • 17
  • 112
  • 190