Connect a SIGNAL to a SIGNAL with more arguments
-
I have a SIGNAL that takes data as arguments and I would Like to connect to another SIGNAL that receives the same data, but also takes a boolean argument. This boolean argument I want to be the state of a checkbox. My idea was something like this:
connect(_periodSelector, SIGNAL(newDateTimes(QDateTime, QDateTime)), this, SIGNAL(changeAbsolutePeriods(QDateTime,QDateTime, bool =_checkbox->isChecked)));How Can I do This?
-
connect(_periodSelector,&_periodSelectorClass::newDateTimes,[this](QDateTime dateTime){ emit this->changeAbsolutePeriods(dateTime,_checkbox->isChecked()); };i'm not sure but maybe something like this will work
-
I have a SIGNAL that takes data as arguments and I would Like to connect to another SIGNAL that receives the same data, but also takes a boolean argument. This boolean argument I want to be the state of a checkbox. My idea was something like this:
connect(_periodSelector, SIGNAL(newDateTimes(QDateTime, QDateTime)), this, SIGNAL(changeAbsolutePeriods(QDateTime,QDateTime, bool =_checkbox->isChecked)));How Can I do This?
@leonardo-M-B by using the "new" connect syntax and a lambda as forth argument
or by going the along way and creating a slot in your class wherein you emit the new signal with the additional parameter
-
connect(_periodSelector,&_periodSelectorClass::newDateTimes,[this](QDateTime dateTime){ emit this->changeAbsolutePeriods(dateTime,_checkbox->isChecked()); };i'm not sure but maybe something like this will work
-
In order to avoid some weird crashes, you should use a context object in your connect statement:
connect(_periodSelector,&_periodSelectorClass::newDateTimes,this,[this](QDateTime dateTime){ emit this->changeAbsolutePeriods(dateTime,_checkbox->isChecked()); };(notice the extra
thisas third argument)Otherwise, if the object pointed to by
thisis destroyed, the connection will not disconnect automatically. Once the lambda is then executed it will have a danglingthispointer. With these kinds of lambdas (that just wrap another function call) it is usually quite easy to find the right context object.