Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling code from a transition in a state chart
QtWS25 Last Chance

Calling code from a transition in a state chart

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 854 Views
  • 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.
  • M Offline
    M Offline
    max22
    wrote on last edited by max22
    #1

    Hello,
    I'm new here and I hope it is the good place to post my question.
    What I would like to do is this. When the "validate" transition is called, I would like to call some code to some stuff and check user input, and allow the transition only if user input is correct, for example. (And I have quite a lot of those transitions to make). I've read the documentation, it is called a guarded transition. But I don't know how to do it with a "drawn" state chart using the state chart editor.
    0_1559229185387_screenshot.png

    If there was a function such as stateChart.connectToTransition("State_1", "Validate", mySlot) it would be perfect.
    And mySlot would be a function returning a boolean, true would allow the transition, and false would forbid the transition.

    Is it possible ?

    edit : (when I say "code", I mean "C++ code")

    JonBJ 1 Reply Last reply
    1
    • M max22

      Hello,
      I'm new here and I hope it is the good place to post my question.
      What I would like to do is this. When the "validate" transition is called, I would like to call some code to some stuff and check user input, and allow the transition only if user input is correct, for example. (And I have quite a lot of those transitions to make). I've read the documentation, it is called a guarded transition. But I don't know how to do it with a "drawn" state chart using the state chart editor.
      0_1559229185387_screenshot.png

      If there was a function such as stateChart.connectToTransition("State_1", "Validate", mySlot) it would be perfect.
      And mySlot would be a function returning a boolean, true would allow the transition, and false would forbid the transition.

      Is it possible ?

      edit : (when I say "code", I mean "C++ code")

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @max22
      I'm not an expert (never having used them!), but perhaps you start from https://forum.qt.io/topic/103405/calling-code-from-a-transition-in-a-state-chart, section Events, Transitions and Guards. The example shows bool QAbstractTransition::eventTest() being used as a guarded transition, to return true/false according as the transition is to proceed or not.

      Seems odd to have to test that at the event level, maybe, but that's what I came across.

      1 Reply Last reply
      0
      • enjoysmathE Offline
        enjoysmathE Offline
        enjoysmath
        wrote on last edited by
        #3

        How did you draw your states? Curious...

        https://github.com/enjoysmath
        https://math.stackexchange.com/users/26327/exercisingmathematician

        1 Reply Last reply
        0
        • M Offline
          M Offline
          max22
          wrote on last edited by
          #4

          @enjoysmath i drew them (very quickly!) with qtcreator... why do you find it curious ?

          @jonB thanks for the advice, but I still don't know how to make the link with the state chart... unfortunately there is little documentation about state charts... maybe I should give up and use the code of your link instead of state charts, but I work with people who don't code, and it would be useful...

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrShawn
            wrote on last edited by
            #5

            Hey,

            So I spent some time with SCXML and State Machine and I can't recommend using SCXML. I am not sure on the progress you made and the time you spent on your SCXML app but trust me using the stardard state machine framework and writing the code will be a lot cleaner.

            With that said I do not suggest checking during your transition but check before your transition occurs, OR transition to a state dedicated to checking. The checking is done in that state and you can either transition back on failure or forward on success.

            If you are using the SCXML state machine you can tap into signals coming from that (I don't have code for you sadly). Which will say when you enter and exit states. You can use those signals connected to slots that will allow you to do run some code and invoke events back into the SCXML state machine from there.

            If you want to use the regular State Machine Framework then you can either override the onEntry() methods for your classes that inherit QState or simple have a manager which will execute slots when states are entered and exited (QState signals for entered and exited). Also transitioning is much more conceptually straight forward with signals for transitions. Also once you get the syntax for State Machine Framework all the complexity of your code will really be in the setup up the state machine and transitions. If you set it up all right it just runs.

            Again I can't really recommend using SCXML, sure you are getting the convenience of a visual tool to set up your state machine (which is somewhat subpar especially one you get a complex state machine), however the games you need to play in code to make it work in my opinion are not clean at all.

            1 Reply Last reply
            2
            • M Offline
              M Offline
              max22
              wrote on last edited by
              #6

              @MrShawn Thanks for your message, it helped me a lot ! sorry for my late reply. Your idea of using a state dedicated to checking was very useful. (I am translating the code of a java project in which i used a big switch statement as a state machine, so the state machine model is not exactly the same).

              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