C++ Qt:检查QStateMachine的当前状态

发布于 2024-12-21 20:20:41 字数 79 浏览 2 评论 0原文

我正在尝试在 Qt (C++) 中实现状态机。 如何检查 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拿命拼未来 2024-12-28 20:20:42

您尝试过 QStateMachine::configuration() 吗?

请参阅http://www.qtcentre.org/threads/42085 -如何获取 QStateMachine 的当前状态

摘自上述网址:

// QStateMachine::configuration() gives you the current states.

while(stateMachine->configuration().contains(s2))
{
     //do something
}

have you tried QStateMachine::configuration() ?

refer 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
}
つ低調成傷 2024-12-28 20:20:42

您可以将该属性分配给 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");

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");
别理我 2024-12-28 20:20:42

我意识到我来晚了,但希望这个答案可以帮助其他偶然发现这个问题的人。

您上面提到您已经尝试使用configuration(),但没有任何状态存在——这是因为start() 是异步的。

因此,假设您在调用 start() 之后立即调用了 configuration(),那么您的状态尚未存在是有道理的。您可以通过使用 QStateMachine 类的started() 信号来获得您想要的功能。检查一下:

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

然后,对于 ReceiveStateMachineStarted() 槽,您可以执行以下操作:

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

当状态机进入初始状态时,它将发出 start() 信号。您编写的插槽将听到该消息并打印配置。有关详细信息,请参阅以下 Qt 文档:

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

I realize I'm coming in late, but hopefully this answer helps anyone else who stumbles across this.

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 you called configuration() immediately after calling start(), it makes sense that your states weren't there yet. You can get the functionality you want by using the started() signal of the QStateMachine class. Check it out:

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

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

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

When your state machine enters its initial state, it will emit the start() signal. The slot you've written will hear that and print the config. For more on this, see the following Qt documentation:

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

长发绾君心 2024-12-28 20:20:42

来自 Qt 5.7 文档

QSet QStateMachine::configuration() const

返回此状态机当前所处的最大一致状态集(包括并行状态和最终状态)。如果状态 s 在配置中,则始终是父状态s 也在 c 中。但请注意,机器本身并不是配置的显式成员。

用法示例:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文