Qt5 信号/时隙语法,带过载信号和 lambda
Qt5 Signal/Slot syntax w/ overloaded signal & lambda
我正在使用Signal/Slot连接的新语法。它对我来说很好,除非我试图连接过载的信号。
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&types),而editTextChanged不是。非过载信号连接良好。过载的一个没有。我想我错过了什么?对于GCC 4.9.1,我得到的错误是
no matching function for call to ‘MyClass::connect(QComboBox*&, <unresolved overloaded function type>, MyClass::setup()::<lambda()>)’
您需要通过如下方式显式选择所需的重载:
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 ) );
相关文章:
- Qt VTK交互风格的信号到小部件
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 可组合的lambda/std::函数与std::可选
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 为什么Qt在信号和插槽中为lambda表达式抛出错误?
- 在Qt信号和插槽中使用lambda语法并访问传递的参数
- 在 for 循环中使用 lambda 函数连接信号插槽
- Qt5 新信号到 lambda 连接内存泄漏
- 将QObject接口信号连接到lambda插槽
- 使用Auto和Lambda处理信号
- Qt信号lambda导致shared_ptr泄漏
- Qt C++将具有非空签名的信号连接到lambda
- 我仍然需要从Qt5.5中的信号断开lambda吗?
- 将QML信号连接到c++ 11 lambda插槽(Qt 5)
- Qt5 信号/时隙语法,带过载信号和 lambda
- 使用 lambda 函数处理 C++ libsigc++ 信号
- Boost信号自动断开lambda槽