Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qt: Check the current State of QStateMachine

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

like image 865
tbienias Avatar asked Dec 15 '11 12:12

tbienias


2 Answers

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");
like image 55
Apivan Tuntakurn Avatar answered Oct 07 '22 01:10

Apivan Tuntakurn


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
}
like image 35
kay Avatar answered Oct 07 '22 00:10

kay