[Qt-interest] Qt state machine: problem while queuing multiple events

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Fri Jun 24 11:33:57 CEST 2011


Hi Peter,

> So the decision if a e3 event should trigger the transition from S2 to S3
> or S3 to S2 is NOT done by the state machine? There should be no translation,
> the decision should be part of the state machine.

The SM does make the decision. Rather I have custom guarded
transitions that make the decision on when to trigger a SM transitions
(as Sean pointed out in his last mail).

>
> Doesn't it work if a S3->S2 transition tests if the event is e3 and only
> then return true, also a S2->S1 which tests on e2. Then you could post
> the events without any translation to the machine and the machine knows
> what to do with the events. For instance if you are in S2 and it comes
> an e3 event it always makes a transition to S3 regardless of how many e3
> events are in the queue, or were in the queue.

Thats how the simple SM i pointed above is connected. Rather all
'forward' transitions are taking place the way you described.

But I have a requirement, which says that on receiving e3 when in S3,
go back to S2. Similar requirement is to go to S1 when receiving e2 in
S2.

The problem is that when in S3, I can receive more than 1 key presses
in quick succession, even before the event posted to the SM gets a
chance to be processed. This is because of the way Qt's event loop is
configured to read 'spontaneous' (key press etc) and 'posted' (post
event to SM event loop) events.

So, because of this there are TWO 'back' events posted to the SM's
event loop waiting to be processed and when it does process them, it
transitions to S2 and then to S1 (since 'back' was a valid event in
both states).

The only valid solution (other than putting hacks in SM to NOT queue
more than 1 event), seems to be to have different events for S3->S2
and S2->S1 transitions.

Hope that explains it.

-mandeep



More information about the Qt-interest-old mailing list