【问题标题】:Qt5 Signal/Slot syntax w/ overloaded signal & lambda带有重载信号和 lambda 的 Qt5 信号/插槽语法
【发布时间】:2015-09-18 19:15:35
【问题描述】:

我正在为信号/插槽连接使用新语法。它对我来说很好,除非我尝试连接过载的信号。

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

不同之处在于 currentIndexChanged 是重载的(int 和 const QString& 类型),但 editTextChanged 不是。非过载信号连接良好。超载的不会。我想我错过了什么?使用 GCC 4.9.1,我得到的错误是

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

【问题讨论】:

标签: c++ lambda qt5


【解决方案1】:

您需要通过以下方式明确选择所需的重载:

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

从 Qt 5.7 开始,提供了方便的宏 qOverload 来隐藏转换细节:

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

【讨论】:

  • 干杯...不确定是否相当复杂是否让专业人士值得......但至少我知道!。
猜你喜欢
  • 2013-11-13
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 2011-11-23
相关资源
最近更新 更多