C++ Qt:检查QStateMachine的当前状态
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试过 QStateMachine::configuration() 吗?
请参阅http://www.qtcentre.org/threads/42085 -如何获取 QStateMachine 的当前状态
摘自上述网址:
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 本身。
然后,可以从动态属性中读取当前状态。
You can assign the property to the QStateMachine itself.
Then, the current state can be read from dynamic property.
我意识到我来晚了,但希望这个答案可以帮助其他偶然发现这个问题的人。
您上面提到您已经尝试使用configuration(),但没有任何状态存在——这是因为start() 是异步的。
因此,假设您在调用 start() 之后立即调用了 configuration(),那么您的状态尚未存在是有道理的。您可以通过使用 QStateMachine 类的started() 信号来获得您想要的功能。检查一下:
然后,对于 ReceiveStateMachineStarted() 槽,您可以执行以下操作:
当状态机进入初始状态时,它将发出 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:
Then, for your ReceiveStateMachineStarted() slot, you could do something like this:
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
来自 Qt 5.7 文档
QSet QStateMachine::configuration() const
返回此状态机当前所处的最大一致状态集(包括并行状态和最终状态)。如果状态 s 在配置中,则始终是父状态s 也在 c 中。但请注意,机器本身并不是配置的显式成员。
用法示例:
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: