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


  • Qt Champions 2016

    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/8

    in such cases i just use the old SLOT & SIGNAL macros as the typecast needed
    is very ugly and confusing to read later.



  • @mrjj I prefer to use the old syntax, this one is really ugly. lol, but thank you.


  • Qt Champions 2016

    @Volebab
    Yeah, when overloaded signals, new syntax is ugly.
    else it looks nice and is type safe
    You can also connect to a function. :)



  • @mrjj You are right, it works nicely with functions without arguments


  • Qt Champions 2016

    @Volebab
    also with arguments as long as its the only one with that name :)



  • 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.



  • @hskoglund That is amazing actually, the second I saw your code I thought about putting it in a global header file and call it the day. Perhaps I would change a few names though. What you mean with PMF?


  • Qt Champions 2016

    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! :-*


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.