C ++ Qt: check the current status of QStateMachine

I am trying to implement a state machine in Qt (C ++). How to check the current status of QStateMachine? I could not find the method in the documentation.

THX

+7
source share
4 answers

Have you tried QStateMachine::configuration() ?

send http://www.qtcentre.org/threads/42085-How-to-get-the-current-state-of-QStateMachine

Excerpt from the above URL:

 // QStateMachine::configuration() gives you the current states. while(stateMachine->configuration().contains(s2)) { //do something } 
+13
source

You can assign a property to QStateMachine itself.

 // QState m_State1; // QState m_State2; // QStateMachine m_Machine; m_State1.assignProperty(m_Label, "visible", false); m_State1.assignProperty(&m_Machine, "state", 1); m_State2.assignProperty(m_Label, "visible", true); m_State2.assignProperty(&m_Machine, "state", 2); 

Then the current state can be read from the dynamic property.

 qDebug() << m_Machine.property("state"); 
+5
source

From Qt 5.7 Documentation

QSet QStateMachine :: configuration () const

Returns the maximum consistent set of states (including parallel and final states) that this finite state machine is in. If the state of s is in the configuration, it always happens that the parent of s is also in c. Note, however, that the machine itself is not an explicit member of the configuration.

Usage example:

 bool IsInState(QStateMachine& aMachine, QAbstractState* aState) const { if (aMachine_.configuration().contains(aState)) return true; return false } 
0
source

I understand that I am coming late, but I hope this answer helps someone else who stumbles about it.

You mentioned above that you already tried to use configuration (), but none of your states were there - this is because start () is asynchronous.

So, assuming that you called configuration () right after calling start (), it makes sense that your states have not existed yet. You can get the necessary functionality using the initial () signal of the QStateMachine class. Check this:

 stateMachine->setInitialState(someState); stateMachine->start(); connect(stateMachine, SIGNAL(started()), this, SLOT(ReceiveStateMachineStarted())); 

Then for your ReceiveStateMachineStarted () slot, you can do something like this:

 void MyClass::ReceiveStateMachineStarted() { QSet<QAbstractState*> stateSet = stateMachine->configuration(); qDebug() << stateSet; } 

When your state machine goes back to its original state, it issues a start () signal. The slot you wrote will hear this and print the configuration. See the following Qt documentation for more on this:

http://doc.qt.io/qt-5/qstatemachine.html#started

0
source

All Articles