[SOLVED] How to pass a signal forwarded parameter into a lambda, defined in the connect statement?



  • I am having difficulties figuring out how to get access to a parameter, passed by a signal in the lambda the signal is connected to. Any ideas?


  • Moderators

    You mean something like this?
    @
    connect(someButton, &QPushButton::toggled, [](bool checked) { if(checked) .... ; });
    @



  • Ah yes, it worked, I wasn't getting any autocomplete so I figured I must have messed something up. Gotta try upgrading to the new Creator.

    @ QObject::connect(&b, static_cast<void (Widget::*)(QMouseEvent *)>(&Widget::clicked), [=](QMouseEvent *ev){
    if (ev->button() == Qt::RightButton) qDebug() << "right";
    });@


  • Moderators

    What's the static_cast for? Wouldn't this suffice?

    @
    QObject::connect(&b, &Widget::clicked, [](QMouseEvent *ev){
    if (ev->button() == Qt::RightButton) qDebug() << "right";
    });
    @



  • Last time I used an overloaded signal or slot the compiler complained it cannot resolve the methods. So I had to use this awkward syntax to get the address of the right overload, otherwise it wouldn't compile.

    I didn't expect this to work at all. But if get it correctly, the lambda parameter helped resolve the right address of clicked, or it just blindly connects to the derived signal instead the one in the base class, but then how does the lambda parameter know where to come from?


Log in to reply
 

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