Important: Please read the Qt Code of Conduct -

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

  • Lifetime Qt Champion

    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

    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.

  • Lifetime Qt Champion

    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

  • Lifetime Qt Champion

    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:


    Example on how to use them:


    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:

    static_cast<void (std::decay<decltype(*QOBJPTR)>::type::*)(ARGS)> \

    Use them for overload functions like this:

    CONNECTSCAST(ui->comboBox,const QString &,currentIndexChanged,this,indexChanged);
    auto sm = new QSignalMapper(ui->pushButton);

    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?

  • Lifetime Qt Champion

    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