Query signal for value in lambda function

  • I am trying to use the following syntax to propagate a signal up my widget hierarchy

      connect(imgSeqCtrl, &ImgSeqCtrlWidget::step,  [this]() {  emit mySignal(1);  });

    where step is a signal with the following signature

    void step(int);

    Now, I want to emit a different signal depending on the sign of the step's signal value. Is there a possibility to query the steps actual value inside the lambda or is there now way around an additional dispatch function in which I do the case query?

  • Qt Champions 2017

    What is the signature of ImgSeqCtrlWidget::step?

  • Yes, you need to change your lambda to:

    [this](int value) { 
    if (value < 0) 
        emit signal1(); 
        emit signal2();

    Basically the lambda needs to take the same (or less) arguments as your initial signal.

  • thank you!

    void ImgSeqCtrlWidget::step(int);

  • @Sewing Just to complete the picture, you don't need a Lambda to propagate a Sigal, you can connect Signals to Signals as long as the arguments are compatible, which theay are in your case:

    connect(imgSeqCtrl, &ImgSeqCtrlWidget::step,  this, &myClassName::mySignal);

  • Yes, but what if I want to send my signal, equipped with two different values?

  • @Sewing well that depends, if you want to add a new argument to your Signal than you'll need a function to do that, that function can be an actual SLOT of your class or a lambda.

    however droping arguments, no problem

    //SignalA(int index, QString text);
    //SignalB(int index);
    //This os ok
    connect(objectA, &&QObject::SignalA, objectB, &QObject::SignalB);
    //This is not
    connect(objectB, &&QObject::SignalB, objectA, &QObject::SignalA);
    //Howver iirc this should work:
    //SignalA(int index, QString text = "DefaultText");
    //SignalB(int index);
    connect(objectB, SIGNAL(SignalB(int)), objectA, SIGNAL(SignalA(int,QString)));

  • thank you!

Log in to reply

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