我正在尝试用Qt (C++)实现一个状态机。如何检查QStateMachine的当前状态?我在文档中找不到方法。
thx
发布于 2011-12-15 20:11:01
你试过QStateMachine::configuration()吗?
参考http://www.qtcentre.org/threads/42085-How-to-get-the-current-state-of-QStateMachine
从上面的url中摘录的:
// QStateMachine::configuration() gives you the current states.
while(stateMachine->configuration().contains(s2))
{
//do something
}发布于 2015-03-18 12:46:24
您可以将该属性分配给QStateMachine本身。
// 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);然后,可以从动态属性中读取当前状态。
qDebug() << m_Machine.property("state");发布于 2017-01-14 22:39:37
来自Qt 5.7 Documentation
QSet QStateMachine::configuration()常量
返回此状态机当前所处的最大一致状态集(包括并行状态和最终状态)。如果状态s在配置中,则s的父级也在c中。但是,请注意,机器本身并不是配置的显式成员。
示例用法:
bool IsInState(QStateMachine& aMachine, QAbstractState* aState) const
{
if (aMachine_.configuration().contains(aState)) return true;
return false
}https://stackoverflow.com/questions/8519890
复制相似问题