QStateMachine: Difference between QEvent and Signal?

1k Views Asked by At

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:

Application state model

  • 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:

  1. In the context of QStateMachine, what is the difference between Signal and QEvent?
  2. How do I define a conditional transition, i.e. on error go to... ?
  3. Should program logic happen in Transition.onTransition() or in QState.onEnter()?

Any pointers are appreciated muchly!

1

There are 1 best solutions below

0
On BEST ANSWER

Ok. Pyside.QtCore.Signal is Signals & Slots derivative.

As for your questions

  1. I'd say there is no difference in QEvent and Signal in context of QStateMachine (although QEvent and Signals & Slots are totally different concepts). Depending on your needs you can trigger transition with QEvent or Signal. See QAbstactTransition for the list of out of the box transitions:

    Inherited by: QSignalTransition, QEventTransition, QMouseEventTransition, QKeyEventTransition

  2. Again depending on what happens inside your application your error may be either signal from QObject or you can send (post) custom QEvent. You'll need to implement your custom QEvent and/or custom QEventTransition to trigger transition only on your events.

  3. 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 inside onEnter(). Also it seems wrong for me to put code that changes state inside onTransition() like in example you've shown:

    def onTransition(self, e):
        x = e.arguments()[0]
        fac = self.fact.fac
        self.fact.fac = x * fac
        self.fact.x = x - 1
    

    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 between UIView's i.e. UI states.