Results 1 to 7 of 7

Thread: How to add a transition in onEntry in Qtstatemachine

  1. #1
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default How to add a transition in onEntry in Qtstatemachine

    Reading the statemachine in qt docs, they say you can override the onEntry() for each state. My question is how to add transistion inside the onEntry function so that the state machine moves to another state from the onEntry().

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add a transition in onEntry in Qtstatemachine

    Create a new QSignalTransition instance and set its target state to the state you want to go to from the onEntry method. Create a new signal for your class to be used by the QSignalTransition. Add this new transition to the state with the onEntry function. In the onEntry function, emit the signal.

    Qt Code:
    1. // MyStateMachine.h
    2.  
    3. signals:
    4. void goToState2();
    5.  
    6. private slots:
    7. void onState1Entered();
    8. void onState2Entered();
    9.  
    10. private:
    11. void setupStates();
    12.  
    13. private:
    14. QStateMachine * pStateMachine;
    15.  
    16. //...
    17.  
    18.  
    19. // MyStateMachine.cpp
    20. void MyStateMachine::setupStates()
    21. {
    22. // pStateMachine is theQStateMachine instance
    23.  
    24. QState * pState1 = new QState( pStateMachine );
    25. connect( pState1, SIGNAL( entered() ), this, SLOT( onState1Entered() ) );
    26.  
    27. QState * pState2 - new QState( pStateMachine );
    28. connect( pState2, SIGNAL( entered() ), this, SLOT( onState2Entered() ) );
    29.  
    30. QSignalTransition * pState1To2 = new QSignalTransition( this, SIGNAL( goToState2() );
    31. pState1To2->setTargetState( pState2 );
    32. pState1->addTransition( pState1To2 );
    33. }
    34.  
    35. void MyStateMachine::onState1Entered()
    36. {
    37. emit goToState2();
    38. }
    To copy to clipboard, switch view to plain text mode 

    As you see, you do not have to create a custom QState and override the onEntry() method. Everything you want to do can almost always be done using standard QState and QStateMachine from the Qt libraries. The code I posted above is adapted from a complex state machine I implemented to handle mouse and keyboard events in a graphics plot. Even though I called the class "MyStateMachine" in this example, it is not derived from QStateMachine. It instantiates a QStateMachine instance ("pStateMachine ") and adds all of the states and transitions to that.
    Last edited by d_stranz; 17th April 2016 at 20:21.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add a transition in onEntry in Qtstatemachine

    @d_stranz: unless the slot does more than just emit the signal you could have used the state's signal directly for the signal transition.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add a transition in onEntry in Qtstatemachine

    Yes, of course. But I think for debugging purposes it is clearer to understand what is happening if you have a handler for the slot where you can set a breakpoint and back trace how it got there. With the state machine I developed, it was complex enough that I often needed debug statements to be able to follow the state transitions, so I developed that convention I posted.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add a transition in onEntry in Qtstatemachine

    Quote Originally Posted by d_stranz View Post
    Yes, of course. But I think for debugging purposes it is clearer to understand what is happening if you have a handler for the slot where you can set a breakpoint and back trace how it got there.
    Good point.

    I usually even have the signal object separate from the object containing the state machine, so that these "internal" signals are not part of the "external" API.

    Cheers,
    _

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add a transition in onEntry in Qtstatemachine

    I usually even have the signal object separate from the object containing the state machine
    I'm not entirely sure I understand what you mean by this. I think what you are saying is that instead of having the class that holds the QStateMachine instance be the one that generates the state transition signals, you would implement another class to implement the signal methods. This class would have a private implementation. Would you make this class a friend of your "public" class so it could emit the signals or would you implement the state machine entirely within the private class?

    I guess I'm trying to understand how it would work in practice.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add a transition in onEntry in Qtstatemachine

    Taking your example, MyStateMachine would have another member of one (or more) classes that serve as signal sources for transitions, if I would like to add logging/break points as you suggested earlier or if the actual trigger needs to be a method call else where.

    One example for the latter would be that you need to expose an object with callable methods to QML so that you can trigger statemachine transitions from there, but only want to expose a subset of such triggers.
    Also applies to only exposing certain transition trigger functions/slots to certain widgets.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    d_stranz (18th April 2016)

Similar Threads

  1. QStataMachine: transition guards
    By nick85 in forum Newbie
    Replies: 5
    Last Post: 12th April 2024, 20:21
  2. listview transition
    By joko in forum Qt Quick
    Replies: 0
    Last Post: 15th June 2015, 17:59
  3. Qt 4.7.3 to Qt 5.1 transition issue
    By Rajesh.Rathod in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2014, 10:39
  4. Replies: 3
    Last Post: 2nd September 2013, 18:01
  5. QtStateMachine in GUI projects
    By dyngoman in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2010, 17:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.