By writing a small demo to complete my post, I found the issue. As it might be helpful to somebody else, here it is:
demo.scxml:
<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" name="plop.scxml" qt:editorversion="4.7.1">
<state id="State_1">
<qt:editorinfo scenegeometry="144.50;162.05;84.50;112.05;120;100" geometry="144.50;162.05;-60;-50;120;100"/>
</state>
</scxml>
demo.py:
#!/usr/bin/env python
import sys
from PySide2.QtCore import QObject, QCoreApplication, SLOT, Slot
from PySide2.QtScxml import QScxmlStateMachine
class Backend_1(QObject):
def __init__(self, machine):
super(Backend_1, self).__init__()
self.machine = machine
self.machine.connectToState("State_1", self, SLOT("s1_active(bool)"))
@Slot(bool)
def s1_active(self, active):
if active:
print('Backend_1::State_1: enter')
self.submachine = QScxmlStateMachine.fromFile('demo.scxml')
b = Backend_2(self.submachine)
self.submachine.start()
else:
print('Backend_1::State_1: exit')
class Backend_2(QObject):
def __init__(self, machine):
super(Backend_2, self).__init__()
self.machine = machine
self.machine.connectToState("State_1", self, SLOT("s1_active(bool)"))
@Slot(bool)
def s1_active(self, active):
if active:
print('Backend_2::State_1: enter')
else:
print('Backend_2::State_1: exit')
if __name__ == '__main__':
app = QCoreApplication(sys.argv)
machine = QScxmlStateMachine.fromFile('demo.scxml')
b = Backend_1(machine)
machine.start()
sys.exit(app.exec_())
If you run this, you expect to see:
Backend_1::State_1: enter
Backend_2::State_1: enter
But, the second statemachine does not seem to start. To fix it, you need to store the instance of Backend_2 (eg: self.b = Backend_2(self.submachine)). I guess it was garbage collected, so all the states/events connections were broken.
Meanwhile, if you think my design patter is bad, I'm willing to learn :)