Qt connect new syntax error
-
I tried to use connect with a signal that has arguments, like this:
connect(response, &QNetworkReply::error, this, &Webservice::error);
But I'm getting an error:
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 *'
Context does not allow for disambiguation of overloaded function -
Hi
If error is overloaded then its why,
( overloaded = more than one error function exists with different parameters)
You need extra (ugly) syntax to force compiler to use the correct one
https://forum.qt.io/topic/20998/qt5-new-signals-slots-syntax-does-not-work-solved/8in such cases i just use the old SLOT & SIGNAL macros as the typecast needed
is very ugly and confusing to read later. -
Hi, I also faced this dilemma; whether to use the old syntax (which is easier) or the new (which gives you better/more error messages when compiling). I wrote 2 macros:
#define COMPOSEPMF(QOBJPTR,FUNC) \ &std::decay<decltype(*QOBJPTR)>::type::FUNC #define CONNECT(SENDER,FUNC1,RECEIVER,FUNC2) \ connect(SENDER,COMPOSEPMF(SENDER,FUNC1),RECEIVER,COMPOSEPMF(RECEIVER,FUNC2))
Example on how to use them:
CONNECT(ui->pushButton,clicked,this,buttonClicked);
So you use them like the old macros but they give you the new syntax (i.e. having the cake and eating it too :-)
Ok, what about using overloading functions? I wrote 3 more macros:
#define COMPOSECASTPMF(QOBJPTR,ARGS,FUNC) \ static_cast<void (std::decay<decltype(*QOBJPTR)>::type::*)(ARGS)> \ (COMPOSEPMF(QOBJPTR,FUNC)) #define CONNECTSCAST(SENDER,ARGS,FUNC1,RECEIVER,FUNC2) \ connect(SENDER,COMPOSECASTPMF(SENDER,ARGS,FUNC1),RECEIVER, \ COMPOSEPMF(RECEIVER,FUNC2)) #define CONNECTRCAST(SENDER,FUNC1,RECEIVER,ARGS,FUNC2) \ connect(SENDER,COMPOSEPMF(SENDER,FUNC1),RECEIVER, \ COMPOSECASTPMF(RECEIVER,ARGS,FUNC2))
Use them for overload functions like this:
ui->comboBox->addItems({"Now","is","the","time"}); CONNECTSCAST(ui->comboBox,const QString &,currentIndexChanged,this,indexChanged); auto sm = new QSignalMapper(ui->pushButton); CONNECTRCAST(ui->pushButton,clicked,sm,void,map);
Ok still complicated, but maybe not as ugly as the new syntax, when dealing with overloaded functions. Also note: you need a C++11 (or later) compiler.
-
Much nicer to look at :)
Thx for sharing- having the cake and eating it too :-)
My favorite concept :)
-
@hskoglund Looks like a really good idea. Thank you so much for sharing! :-*