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?
QtWS25 Last Chance

[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 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
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #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 last edited by Chris Kawa
        #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
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #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 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

            • Login

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