I'm currently porting a small application from PyGTK to PySide which sits in your systray and periodically checks a server for updates.
When I initially wrote this, I used a custom state model to represent its behaviour:
- init: initial state
- check: check server for updates
- disconnected: no connection to server (wait for some time to try again)
- error: server reported an error (user needs to check settings before proceeding)
- idle: wait for POLLING INTERVAL seconds before checking again
- quit
I since discovered that Qt has QStateMachine, which seems perfect for this type of structure. However, I was not able to apply the examples satisfyingly to my problem!
In particular:
- In the context of QStateMachine, what is the difference between Signal and QEvent?
- How do I define a conditional transition, i.e. on error go to... ?
- Should program logic happen in Transition.onTransition() or in QState.onEnter()?
Any pointers are appreciated muchly!
Ok.
Pyside.QtCore.Signal
is Signals & Slots derivative.As for your questions
I'd say there is no difference in
QEvent
andSignal
in context of QStateMachine (althoughQEvent
and Signals & Slots are totally different concepts). Depending on your needs you can trigger transition withQEvent
orSignal
. SeeQAbstactTransition
for the list of out of the box transitions:Again depending on what happens inside your application your error may be either signal from
QObject
or you can send (post) customQEvent
. You'll need to implement your customQEvent
and/or customQEventTransition
to trigger transition only on your events.And again it depends:) Transition is the glue. It has knowledge about source and destination states. So I'd put only preparatory code inside
onTransition()
and state initialization code insideonEnter()
. Also it seems wrong for me to put code that changes state insideonTransition()
like in example you've shown:but as you can see it works well.
NB: If you have iOS experience then
UIStoryboardSegue
is analogue for transition. Mainly it is used to pass data betweenUIView
's i.e. UI states.