Qt5 Signal/Slot syntax w/ overloaded signal & lambda

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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()>)’ 

回答1:

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 ) ); }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!