Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to pass a signal forwarded parameter into a lambda, defined in the connect statement?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 13.5k Views
  • 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 Offline
    U Offline
    utcenter
    wrote on 27 Mar 2013, 13:44 last edited by
    #1

    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
    1
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 27 Mar 2013, 16:00 last edited by Chris Kawa 1 Aug 2019, 13:04
      #2

      You mean something like this?

      connect(someButton, &QPushButton::toggled, [](bool checked) { if(checked) .... ; });
      
      1 Reply Last reply
      2
      • U Offline
        U Offline
        utcenter
        wrote on 27 Mar 2013, 17:21 last edited by Chris Kawa 1 Aug 2019, 13:05
        #3

        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
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 27 Mar 2013, 17:27 last edited by Chris Kawa 1 Aug 2019, 13:04
          #4

          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
          0
          • U Offline
            U Offline
            utcenter
            wrote on 27 Mar 2013, 17:46 last edited by
            #5

            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
            0

            1/5

            27 Mar 2013, 13:44

            • Login

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