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. Changing state of QStateMachine based on a signal argument.
Qt 6.11 is out! See what's new in the release blog

Changing state of QStateMachine based on a signal argument.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 8.2k 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.
  • M Offline
    M Offline
    maciej
    wrote on last edited by
    #1

    In my project I'm using QStateMachine to control GUI. It works fine, but lately I've run on one problem. I've got signal @finished(ushort status, QString msg);@ , which informs that worker had finished action (and if it succeed or failed). Transition is based on this signal, and it should choose different states based on success or failure. How should I achieve that? Is defining 2 signals (succes(), fail()) only solution?

    Earth is a beta site.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      No need to define two signals.

      What I would do is to make the two transitions guarded transitions. This means that for these transitions you should derive your own custom transition class from QSignalTransition and override the bool QAbstractTransition::eventTest( QEvent* e ) function.

      This function gets called by the state machine framework when checkign which transitions should occur (ie each time an event is delivered and the SM is in the source state of the transition). If the transition meets all of your criteria then return true, otherwise return false.

      In your simple case a single class with a settable boolean property that lets each object know if it should be checking for success or failure is all that is needed. In more complex cases you may need a special transition class for each possible transition. It depends on how easily your tests can be abstracted.

      In your case something like the following should do the trick:

      @
      class BooleanSignalTransition : public QSignalTransition
      {
      Q_OBJECT
      BooleanSignalTransition( QState* sourceState, const char* signal, QAbstractState* targetState )
      : QSignalTransition( sourceState, signal, targetState )
      m_test( true ) // Check for success by default
      {}

      void setTest( bool b ) { m_test = b; }
      bool test() const { return m_test; }
      
      virtual bool eventTest( QEvent* e )
      {
          // Let QSignalTransition check it is the correct signal first of all
          if ( !QSignalTransition::eventTest( e ) )
              return false;
      
          // Now check that the argument has the correct value
          QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>( e );
          bool value = se->arguments().at( 0 ).value<bool>();
          return ( value == m_test );
      }
      

      private:
      bool m_test;
      };
      @

      Then when you construct the states in question make two instances of the above instead of your usual QSignalTransition objects and for one of them call setTest( false ).

      Hope that helps.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      1
      • M Offline
        M Offline
        maciej
        wrote on last edited by
        #3

        nice post. thanks

        Earth is a beta site.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          You're welcome. I have the above to be a very common pattern with state machines. Often though the transitions guard against receiving a specific event type rather than a signal. See this "doc note":http://developer.qt.nokia.com/doc/qt-4.7/statemachine-api.html#note-36 for some helper classes to do this easily.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bjocar
            wrote on last edited by
            #5

            Good answer indeed. Just wish I had found it sooner...

            To me it seems like a common problem when using state machines, adding the example to the documentation on the state machine framework wouldn't hurt.

            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