Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Custom QEvent for QStateMachine : type recognition won't work
Forum Updated to NodeBB v4.3 + New Features

Custom QEvent for QStateMachine : type recognition won't work

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 3 Posters 375 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tomviow
    wrote on last edited by tomviow
    #1

    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)
    1 Reply Last reply
    0
    • T Offline
      T Offline
      tomviow
      wrote on last edited by tomviow
      #2

      @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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Do you have a singleton instance of your state machine ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        T 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          Do you have a singleton instance of your state machine ?

          T Offline
          T Offline
          tomviow
          wrote on last edited by tomviow
          #4

          @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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved