Qt5 Signal / slot syntax with overloaded signal and lambda

I am using the new syntax for Signal / Slot connections. It works great for me, except when I try to connect a signal that is 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 that currentIndexChanged is overloaded (int and const QString & types), but editTextChanged is not. No overload signal is connected normally. No overloaded. Perhaps I missed something? As of GCC 4.9.1 the error I am getting is this

no matching function for call to β€˜MyClass::connect(QComboBox*&, <unresolved overloaded function type>, MyClass::setup()::<lambda()>)’

      

+4


source to share


1 answer


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, a macro is provided to hide the casting details: qOverload

qOverload

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

      

+10


source







All Articles