Unsolved Custom QEvent for QStateMachine : type recognition won't work
-
Hello everyone,
I have a problem I've been struggling with for a few hours now. I have a QStateMachine controlling a GUI and its screens, and I wanna link this SM to the socket for a TCP/IP communication my software needs.Therefore, my aim is to post a custom event (ConnectionEvent) to the state machine (GUIStateMachine) in order to process a custom transition (ConnectionTransition). I followed the example of the documentation (https://doc.qt.io/qtforpython/overviews/statemachine-api.html#events-transitions-and-guards)
The problem is, once I post the event from my connection-related class, the eventTest method from ConnectionTransition is unable to get the type of my custom event (defined to QEvent.User + 2). It returns PySide2.QtCore.QEvent.Type.None_.
If you have any advice, would be very appreciated ! Thank you
Here are my custom types :
class ConnectionEvent(QEvent): def __init__(self, connected): print("ConnectionEvent") super(ConnectionEvent, self).__init__(QEvent.Type(QEvent.User + 2)) self.setAccepted(connected) class ConnectTransition(QAbstractTransition): def __init__(self, connected): super(ConnectTransition, self).__init__() self.state = connected def eventTest(self, event) -> bool: if event.type() != QEvent.Type(QEvent.User + 2): print("Unrecognized type") print(event.type()) #there I get PySide2.QtCore.QEvent.Type.None_ return False else: print("Recognized type") return self.state == event.isAccepted()
The code defining my transitions in GUIStateMachine:
t1 = ConnectTransition(True) t1.setTargetState(self.states["CONNECTED"]) self.states["WAITING"].addTransition(t1) t2 = ConnectTransition(False) t2.setTargetState(self.states["CONNECTION"]) self.states["CONNECTED"].addTransition(t2)
And finally the code posting the event :
def setConnected(self, value): # some code print("posting event") event = sm.ConnectionEvent(value) sm.GUIStateMachine.getInstance().postEvent(event)
-
@Denni-0 said in Custom QEvent for QStateMachine : type recognition won't work:
View
Thank you for your answer.
I see your point : I'll have to implement an intermediary controller class between my socket (pure python) and my QStateMachine. To be fair, I'm still at the step of creating/conceiving the code, and I was planning on doing this next.However, the thing that worries me is that I'm not able to retrieve the data I want from my custom QEvent. It's supposed to be working in C++ but I don't know if there is a specificity about Python that I'm missing, concerning "abstract classes method parameters" (here, event from eventTest method which is QEvent) and "inherited classes method arguments" (here, my ConnectionEvent that I post to the SM). It's hard to be clear but I hope you (or someone maybe?) get my point.
-
Hi and welcome to devnet,
Do you have a singleton instance of your state machine ?
-
@SGaist I do have a singleton instance of my state machine.
@Denni-0 I completely agree with you. I'll work around a controller which will be the intermediary layer between my components.
Eventually, I found out how to link my socket to my GUI: I just created a "smart boolean" class (QObject), from which I emit signals when the value changes from false to true, or from true to false. With these signals, I can proceed to the transitions and actions I want.