Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

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

    General and Desktop
    2
    5
    11171
    Loading More Posts
    • 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.
    • U
      utcenter last edited by

      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?

      1 Reply Last reply Reply Quote 1
      • Chris Kawa
        Chris Kawa Moderators last edited by Chris Kawa

        You mean something like this?

        connect(someButton, &QPushButton::toggled, [](bool checked) { if(checked) .... ; });
        
        1 Reply Last reply Reply Quote 1
        • U
          utcenter last edited by Chris Kawa

          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";
                  });
          
          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by Chris Kawa

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

            QObject::connect(&b, &Widget::clicked, [](QMouseEvent *ev){
                        if (ev->button() == Qt::RightButton) qDebug() << "right";
                    });
            
            1 Reply Last reply Reply Quote 0
            • U
              utcenter last edited by

              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?

              1 Reply Last reply Reply Quote 0
              • First post
                Last post