I'm trying to implement a state-machine in Qt (C++). How can I check the current state of the QStateMachine? I couldn't find a method in the documentation.
thx
You can assign the property to the 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 dynamic property.
qDebug() << m_Machine.property("state");
From Qt 5.7 Documentation
QSet QStateMachine::configuration() const
Returns the maximal consistent set of states (including parallel and final states) that this state machine is currently in. If a state s is in the configuration, it is always the case that the parent of s is also in c. Note, however, that the machine itself is not an explicit member of the configuration.
Example usage:
bool IsInState(QStateMachine& aMachine, QAbstractState* aState) const
{
if (aMachine_.configuration().contains(aState)) return true;
return false
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With