7

I'm using the new syntax for Signal/Slot connections. It works fine for me, except when I try to connect a signal that's overloaded.

MyClass : public QWidget
{
    Q_OBJECT
public:
    void setup()
    {
        QComboBox* myBox = new QComboBox( this );
        // add stuff
        connect( myBox, &QComboBox::currentIndexChanged, [=]( int ix ) { emit( changedIndex( ix ) ); } ); // no dice
        connect( myBox, &QComboBox::editTextChanged, [=]( const QString& str ) { emit( textChanged( str ) ); } ); // this compiles
    }
private:

signals:
    void changedIndex( int );
    void textChanged( const QString& );
};

The difference is currentIndexChanged is overloaded (int, and const QString& types) but editTextChanged is not. The non-overloaded signal connects fine. The overloaded one doesn't. I guess I'm missing something? With GCC 4.9.1, the error I get is

no matching function for call to ‘MyClass::connect(QComboBox*&, <unresolved overloaded function type>, MyClass::setup()::<lambda()>)’
kiss-o-matic
  • 1,111
  • 16
  • 32
  • 1
    Possible duplicate of [Connecting overloaded signals and slots in Qt 5](http://stackoverflow.com/questions/16794695/connecting-overloaded-signals-and-slots-in-qt-5) – cmannett85 Apr 22 '16 at 14:24

1 Answers1

15

You need to explicitly select the overload you want by casting like this:

connect(myBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [=]( int ix ) { emit( changedIndex( ix ) ); });

Since Qt 5.7, the convenience macro qOverload is provided to hide the casting details:

connect(myBox, qOverload<int>(&QComboBox::currentIndexChanged), [=]( int ix ) { emit( changedIndex( ix ) );
Robert
  • 767
  • 8
  • 17