Qt signal transition on parent state overriden by child state
-
In my
QStateMachine, I have aQState parentthat has several children stateschild1,child2andchild3. I want all children to have a common response to the signalMyStruct::mySignal(). However, I also wantchild1to do something more on that signal. I thought of creating aQSignalTransitionon the parent with the common response, and creating another one with the specific behavior for child1 (see the code below).struct MyStruct { Q_OBJECT Q_SIGNAL void mySignal(); void commonResponse() { std::cout << "Common" << std::endl; } void specificResponse() { std::cout << "Specific" << std::endl; } }; QState* parent = new QState(); QState* child1 = new QState(parent); QState* child2 = new QState(parent); QState* child3 = new QState(parent); MyStruct myStruct; QSignalTransition* commonTransition = new QSignalTransition(&myStruct, &MyStruct::mySignal); parent->addTransition(commonTransition); connect(commonTransition , &QAbstractTransition::triggered, &myStruct, &MyStruct::commonResponse); QSignalTransition* specificTransition = new QSignalTransition(&myStruct, &MyStruct::mySignal); child1->addTransition(specificTransition); connect(specificTransition, &QAbstractTransition::triggered, &myStruct, &MyStruct::specificResponse);However with this code, if I am currently in state
child1and aMyStruct::mySignalis emitted, then it will only output Specific, because the transition is overriden. Is it possible to get the expected behavior of printing both Common and Specific while keeping the code clarity? (without adding a duplicateMyStruct::mySignalfor the specific case, or having to "remember" to callcommonResponseinspecificResponse)It's too bad there is not some kind of parameter on the
QSignalTransitionthat would act like "do not override the transition and allow new similar ones to be executed along with this one".It is a duplicate of my original question on StackOverflow. Maybe you guys have a solution for me. Or at least maybe the devs will hear what I want to do and maybe add that feature :)