QScxmlStateMachine: how to pass arguments to events



  • Hi all,

    reading [1] (The receiver’s method may take a PySide2.QtScxml.QScxmlEvent as a parameter) and [2] (and data as the payload data), I understand that I must be able to pass some data to an event.

    The following basic example works, but MY_EVENT does not take arguments. If I try to pass a bool argument (@Slot(bool) and SLOT('my_event(bool)')), at startup I get:

    QObject::connect: Incompatible sender/receiver arguments
            QScxmlInternal::ScxmlEventRouter::eventOccurred(QScxmlEvent) --> Backend::my_event(bool)
    

    If I try to pass a QScxmlEvent argument (@Slot(QScxmlEvent) and SLOT('my_event(QScxmlEvent)')), I get:

    QObject::connect: No such slot Backend::my_event(QScxmlEvent)
    

    What am I doing wrong? How can I pass argument to an event?

    Thx for your help

    [1] https://doc-snapshots.qt.io/qtforpython/PySide2/QtScxml/QScxmlStateMachine.html#PySide2.QtScxml.PySide2.QtScxml.QScxmlStateMachine.connectToEvent
    [2] https://doc-snapshots.qt.io/qtforpython/PySide2/QtScxml/QScxmlStateMachine.html#PySide2.QtScxml.PySide2.QtScxml.QScxmlStateMachine.submitEvent

    demo4.py

    #!/usr/bin/env python
    
    import sys
    
    from PySide2.QtCore import QObject, QCoreApplication, SLOT, Slot
    from PySide2.QtScxml import QScxmlStateMachine, QScxmlEvent
    
    
    class Backend(QObject):
        def __init__(self, machine):
            super(Backend, self).__init__()
            self.machine = machine
            self.machine.connectToState('StateMachine', self, SLOT('state_machine_active(bool)'))
            self.machine.connectToEvent('MY_EVENT', self, SLOT('my_event()'))
    
        @Slot(bool)
        def state_machine_active(self, active):
            print('StateMachine::active: %s' % active)
            self.machine.submitEvent('MY_EVENT')
    
        @Slot()
        def my_event(self, *args, **kwargs):
            print('MY_EVENT:', args, kwargs)
    
    
    if __name__ == '__main__':
        app = QCoreApplication(sys.argv)
    
        machine = QScxmlStateMachine.fromFile('demo4.scxml')
        b = Backend(machine)
        machine.start()
    
        sys.exit(app.exec_())
    

    demo4.scxml

    <?xml version="1.0" encoding="UTF-8"?>
    <scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" name="demo4.scxml">
        <transition type="internal" event="MY_EVENT"/>
        <state id="StateMachine"/>
    </scxml>
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.