[Qt-interest] Qt state machine: problem while queuing multiple events
Peter Kümmel
syntheticpp at gmx.net
Thu Jun 23 21:49:35 CEST 2011
On 22.06.2011 14:51, Mandeep Sandhu wrote:
> Hi All,
>
> I was experiencing a problem in my state machine and wanted to
> understand how/when events posted to other event loops processed.
>
> I'll illustrate the issue with a simple statemachine.
>
> S1 --e2--> S2 -- e3 --> S3
> /\ /\ |
> |__back___|__back___|
>
> (dia best viewed with fixed-width font!:)
>
> Most of the transitions are pretty simple. I'll explain the 'back'
> event transition a little more.
>
> In S3, if I receive e3 I post a 'back' event to the state machine
> which takes me to S2. Similarly, generate a 'back' event, if I receive
> e2 while in S2. This check is done right before I'm about to post the
> event. That is I determine, based on the current state of the SM,
> whether to post the event as-is or to translate it to 'back'.
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.
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.
I assume if the translation is part of the state machine (means there
are corresponding transitions) the queue works as expected.
Or does this really not work?
Peter
>
> Assume that e2 and e3 are tied to keyboard keys 2 and 3.
>
> Now the SM works fine if I press keys 'slowly' :) I.e I press 2, then
> 3, then keep pressing 3 it toggles between S2 and S3.
>
> However, pressing key 3 quickly can result in the SM landing in S1,
> even though only 3 key is being pressed.
>
> This happens because, when in S3, my keyboard events are coming
> rapidly and I receive 3 more than once even before I make a
> transition. So because of the translation I end up queuing TWO back
> events in the SM's event queue. And not surprisingly, end up in S1 (S2
> -back-> S1).
>
> I was initially under the impression that once I've posted my events
> to the SM event queue, any subsequent key-press event will be
> processed after the SM event is processed. But thats not correct as
> this old Qt article explains:
>
>
> There a couple os solutions to prevent this from happening...but they
> all look a little hacky to me:
>
> 1. Enqueue only a single event to the SM. QSM itself provides no way
> to do that. So I'll have to manage my own queue outside of it. In this
> solution I have to rely on knowing when an event has been
> consumed/rejected by the SM.
> 2. Disable key handling after posting an event. This would however
> require me to know which events are valid and which one's aren't
> (again I'll have to rely on some way of knowing whether the event was
> accepted/rejected).
>
> In both cases, this means using some undocumented (public) QSM API's
> like overloading endMicrostep() etc.
>
> Is there any other way to handle this issue?
>
> Thanks,
> -mandeep
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list