Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Connect signal lambdas trouble

Connect signal lambdas trouble

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hextank
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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!");
      });
      @

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hextank
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved