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. How to pass parameters between states of a QStateMachine
QtWS25 Last Chance

How to pass parameters between states of a QStateMachine

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.8k 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.
  • N Offline
    N Offline
    neterror
    wrote on last edited by
    #1

    Hello,

    I was wondering, what is the proper way of passing parameters between states in a QStateMachine. The transitions between states are triggered by signals, but the signal parameters are not passed to the other QState - only entered() is emitted without options to specify some input data.
    One way I can think to solve the problem is to make QSignalTransition subclass, take the signal parameters in onTransition(QEvent* event) and set them to the targetState() using assignProperty() or some custom method of QState descendant.

    Is there some default solution from QStateMachine framework which I am missing?

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

      Hi,

      What kind of parameter are you thinking about ? On what kind of object would they work ?

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

      1 Reply Last reply
      0
      • N Offline
        N Offline
        neterror
        wrote on last edited by
        #3

        QByteArray parameters. My task is to implement a network protocol working over TCP. I have a class SendMessage:public QState that wraps the state machine for sending a single message (transitions of the QTcpSocket for bytesWritten(), readyRead(), timeout()). I want to combine these states into higher level protocol state machine. When it leaves a state and enters another state it should pass what data it has received.

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

          Might be easier to have a central object that handles the network part and your state machine acts on it so you would be able to retrieve the information needed from that "central point"

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

          1 Reply Last reply
          1
          • N Offline
            N Offline
            neterror
            wrote on last edited by
            #5

            Thanks for your feedback.
            I was trying to think of a more general solution that I may easily specialise to the different network sessions that I have to implement. I think that the result from one state to be passed as input to the next state as part of the transition is a more natural flow of the state machine (without some global state maintenance). I can implement it with by specialising QSignalTransition, I was just curious if there wasn't something ready made.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              frankiefrank
              wrote on last edited by
              #6

              If anyone else worked on this aspect of state machines, would appreciate input - I'm wondering about the same issue.

              "Roads? Where we're going, we don't need roads."

              1 Reply Last reply
              0
              • N Offline
                N Offline
                neterror
                wrote on last edited by
                #7

                For my problem, I did the following:
                Created a subclass of QState, that implements the virtual method QState::onEntry(...) where it takes the parameter from the signal (QByteArray in my case) and keeps it in a local member:

                @
                class ParamState : public QState
                {
                public:
                QByteArray data() {return mData;}
                protected:
                virtual void onEntry(QEvent* e);
                QByteArray mData;
                };

                void ParamState::onEntry(QEvent* e)
                {
                if (e->type() == QEvent::StateMachineSignal) {
                QStateMachine::SignalEvent* se = static_castQStateMachine::SignalEvent*(e);
                if (se->arguments().size() > 0) {
                QVariant arg = se->arguments().at(0);
                if (arg.canConvert(QMetaType.QByteArray))
                mData = arg.toByteArray();
                }
                }
                }
                @

                Each state in my state machine is ParamState instance. In the slots for the QState::entered signal I use the following:
                @
                ParamState* state = (ParamState*)sender();
                state->data() ... <--- the QByteArray that was passed to the state via the transition signal
                @

                1 Reply Last reply
                2
                • F Offline
                  F Offline
                  frankiefrank
                  wrote on last edited by
                  #8

                  Thanks, this is a helpful example!

                  "Roads? Where we're going, we don't need roads."

                  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