Qt5信号/时隙语法w /过载信号& lambda [英] Qt5 Signal/Slot syntax w/ overloaded signal & lambda

查看:396
本文介绍了Qt5信号/时隙语法w /过载信号& lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的语法用于信号/插槽连接。

  MyClass:public QWidget 
{
Q_OBJECT
public:
void set()
{
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:

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

不同的是currentIndexChanged是重载的(int和const QString& types),但editTextChanged不是。非过载信号连接良好。重载的没有。我想我错过了什么?使用GCC 4.9.1,我得到的错误是

 没有匹配的函数调用'MyClass :: connect(QComboBox *& ;<未解析的重载函数类型>,MyClass :: setup()::< lambda()>)'


<

 <$> 

您需要明确选择所需的重载, c $ c> connect(myBox,static_cast< void(QComboBox :: *)(int)>(& QComboBox :: currentIndexChanged),[=](int ix){emit(changedIndex(ix));});


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

解决方案

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

这篇关于Qt5信号/时隙语法w /过载信号&amp; lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆