简体   繁体   English

无法将QButtonGroup buttonClicked连接到仿函数

[英]Cannot connect QButtonGroup buttonClicked to a functor

I am having trouble with connecting QButtonGroup's signal to a functor. 我将QButtonGroup的信号连接到仿函数时遇到问题。

functionGroup = new QButtonGroup();
functionGroup->addButton( ui->pushButtonSetupExperiment, 0);
functionGroup->addButton( ui->pushButtonConfigure, 1);
functionGroup->addButton( ui->pushButtonModify, 2);
functionGroup->addButton( ui->pushButtonSearch, 3);
functionGroup->addButton( ui->pushButtonLogout, 4);

/* This works:
connect( ui->pushButtonSetupExperiment, &QPushButton::clicked, [=]() {
    emit finished(0);
});
*/

// This fails:
connect( functionGroup, &QButtonGroup::buttonClicked, [=](int id) {
    emit finished(id);
});

Following Compiler error occur: 发生以下编译器错误:

error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const' : Cannot convert argument 2 from 'overloaded-function' to 'const char *'...

Can't figure out what's wrong. 无法弄清楚出了什么问题。 Sure I can use the old syntax to complete the task, but I need to learn what's wrong here. 当然,我可以使用旧语法来完成任务,但我需要了解这里有什么问题。 Thanks for helping me! 谢谢你的帮助!

Qt version: 5.5.1 Qt版本:5.5.1
Compiler: msvc2012 编译器:msvc2012

The issue is that there are two overloads for QButtonGroup::buttonClicked , and it cannot disambiguate which one you mean, so it generates the compile error. 问题是QButtonGroup::buttonClicked有两个重载,它无法消除你的意思,因此它会产生编译错误。 To get around this, you need to use static_cast to indicate which variant of buttonClicked you want to use. 要解决此问题,您需要使用static_cast来指示要使用哪个buttonClicked变体。 Unfortunately, the syntax is a bit clunky: 不幸的是,语法有点笨拙:

connect(functionGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), ...);

For more information, see to Differences between Slot Connections docs page as linked by @Devopia. 有关更多信息,请参阅@Devopia链接的Slot Connections文档页面之间的差异。 Depending on your personal opinion you might want to use the old string-based connection syntax for this (ie value conciseness and readability over type safety). 根据您的个人意见,您可能希望使用旧的基于字符串的连接语法(即值简洁性和可读性而不是类型安全性)。 Or you could use a macro or using type alias to try and simplify things. 或者您可以使用宏或using类型别名来尝试简化操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM