PDA

View Full Version : How to know the signal which caused a qstate transition



ClintEastwood
23rd April 2013, 19:03
I have defined a qstatemachine which contains a qstate q1 that can be accessed from several different qstates, however, once reached the qstate q1 the program behaviour must be different depending on the signal which invoke the transition, nevertheless I do not know how to find out which signal caused the transition, since the entered signal of the qstate does not report this.
Moreover, sometimes I can access the qstate q1 from the same state q2 by different signals (so it's not enough for me to know what the latest state was, I need to know which signal caused the transition)

Does anyone know how to do that?

wysota
23rd April 2013, 19:20
You can define separate transitions for different origin states leading to the same state.

ClintEastwood
23rd April 2013, 23:30
That could be a solution but it would highly increase the number of states of my system, and, unfortunately, my state machine already has around 40 states. If there is no other choice I will do it but I prefer to ask a bit more to find a better solution.

My system starts from a initial state that only waits for different commands from the user (for example, start process1, start process2, start process3...). Every time this state receives a command, the workflow is more or less the same, the statemachine jumps to a new state called "waiting Ack", and there, a ping is sent (via socket) to the system in charge of executing the selected process. Once an ack answer is received from the system pinged, the statemachine jumps to the state associated to execute the process selected and once finished it returns to the initial state. According to your solution I should have as many "waiting ack" states as types of process are, but if I could know what transition lead to the single "waiting Ack" state, I could unify all the "waiting Ack" small states in only one by inserting a switch in the entered state slot.

Any idea or suggestion to design my system?

wysota
23rd April 2013, 23:54
That could be a solution but it would highly increase the number of states of my system
No, not really.


QState *s1 = ...;
QState *s2 = ...;
QSignalTransition *t1 = s1->addTransition(obj1, SIGNAL(signal1()), s2);
QSignalTransition *t2 = s1->addTransition(obj2, SIGNAL(signal2()), s2);

connect(t1, &QAbstractTransition::triggered, []() { qDebug() << "t1 fired"; }); // C++11
connect(t2, &QAbstractTransition::triggered, []() { qDebug() << "t1 fired"; }); // C++11

anda_skoa
24th April 2013, 11:59
Alternatively you could have separete "start process" states, each using assignProperty to set the correct recipient for the ACK step, e.g. network address and port number.
Then you transition into a single waiting ACK state which uses the previously set value.

Cheers,
_

ClintEastwood
24th April 2013, 12:07
That looks promissing! I'm going to try it. Thank you!

ClintEastwood
24th April 2013, 16:27
I'm trying to compile the following code according to wysota suggestion and I'm getting an error:



QState *s1 = new QState();
QState *s2 = new QState();
QSignalTransition *t1 = s1->addTransition(this, SIGNAL(t1_sg()), s2);
QObject::connect(t1, SIGNAL(triggered()), this, SLOT(t1_slot()));



This is the error:

'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : can't convert from 'QSignalTransition *' to 'const QObject *'

As far as I know QSignalTransition inherits from QObject, why can't it convert it?

I'm trying to compile the following code according to wysota suggestion and I'm getting an error:



QState *s1 = new QState();
QState *s2 = new QState();
QSignalTransition *t1 = s1->addTransition(this, SIGNAL(t1_sg()), s2);
QObject::connect(t1, SIGNAL(triggered()), this, SLOT(t1_slot()));



This is the error:

'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : can't convert from 'QSignalTransition *' to 'const QObject *'

As far as I know QSignalTransition inherits from QObject, why can't it convert it?

Added after 11 minutes:

I have found the problem, I had included this:



#include <QAbstractTransition>


instead of:



#include <QSignalTransition>