I am having trouble with connecting QButtonGroup's signal to a functor.
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
Compiler: 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. To get around this, you need to use static_cast
to indicate which variant of buttonClicked
you want to use. 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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.