I am designing an application where the control flow between very simple screens is governed by a StateMachine.

The application starts off in stateLogin. Upon entry to that state it displays a screen asking the user to insert a smart card into a smart card reader attached to the system or click a "shutdown" button.

Clicking the shutdown button takes the statemachine to stateShutdown, that part is trivial.

What is the best way to define the transition to stateCardInserted? Obviously I want the statemachine to transition into that state when the user has inserted his card.

The card reader is connected on a serial port and I can query the current status "card in"/"no card in" via commands to that hardware. So I need to poll. Qt has no knowledge of that device.

Also the user could have anticipated this and already inserted a card while the system starts up. So if I concoct some threading scheme to generate events when the status in/out changes it is possible that there is no such event being generated while the system is in stateLogin unless the user takes the card out and puts it back in again.

So far I can only think of defining an unconditional Transition from stateLogin to stateLogin itself. As far as I understand the system this will never actually leave stateLogin but fire continually. I could make that transition a custom subclass of QAbstractTransition, override onTransition() and do my polling there. If a card is inserted I would then post a custom event "cardIsIn" to the machine, and create a QEventTransition to stateCardInserted checking for cardIsIn.

Would that work? Would I have to override eventTest() in my polling transition as well to disable it for my cardisin-event so that it would "break the loop"?

Is there a better way?

Thanks for reading this!

MM, Qt newbie