Connect signal lambdas trouble



  • I'm trying to hook a couple of lambdas to connect for some sig/slots, one works but the other won't compile.

    This is the working one.

    @QProcess *process = new QProcess(this);
    connect(process, &QProcess::readyReadStandardOutput, {
    qDebug("StdOut!");
    });@

    and this one won't compile

    @ connect(process, &QProcess::finished, [](int exitCode, QProcess::ExitStatus exitStatus) {
    qDebug("Done!");
    });@

    I'm 100% sure the lambda signature matches but not sure why it's picking one connect overload over the working one. Any ideas?

    @D:\Dev\SanityCheck\sanity.cpp:30: error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const' : cannot convert parameter 2 from 'overloaded-function' to 'const char *'
    Context does not allow for disambiguation of overloaded function@

    This is all using VS2012 as the compiler. Any pointers appreciated.


  • Moderators

    Hi, and welcome to the Qt Dev Net!

    You need to make an explicit cast to tell your compiler which overload to use.

    @
    void (QProcess::*mySignal)(int, QProcess::ExitStatus) = &QProcess::finished;

    connect(process, mySignal, [](int exitCode, QProcess::ExitStatus exitStatus)
    {
    qDebug("Done!");
    });
    @

    Above, mySignal is the name of a local variable (a function pointer). You can use a different name if you want.

    Or, you can also do an inline cast without storing the function pointer:
    @
    connect(process,
    static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
    [](int exitCode, QProcess::ExitStatus exitStatus)
    {
    qDebug("Done!");
    });
    @



  • Ah thanks, I couldn't see the woods for the trees, I'm guessing this would actually work in Clang. :)


Log in to reply
 

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