c++ - 如何在 Qt SCXML 状态图中使用条件转换
问题描述
我目前正在尝试了解 Qts scxml 状态图,以及如何将它们正确集成到我的应用程序中。我偶然发现的一个问题是条件转换。为了解释我如何在这里使用条件以及出了什么问题,我做了一个最小的可行示例:
初始状态s_initial
有两个转换到状态s_false
和s_true
。两个转换都是由同一个事件触发的t_button_clicked
。根据变量test_var
,任何时候都可能只有一个转换。当另一个t_button_clicked
事件发生时,状态机返回到s_initial
。
为了测试状态机,我创建了一个简单的 Qt-Widgets 应用程序,其中一个按钮触发t_button_clicked
,一个复选框更改变量test_var
:
(主窗口.cpp)
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, chart(this)
{
ui->setupUi(this);
connect(ui->checkBox, &QCheckBox::clicked, [this](bool checked){
qDebug() << "> checkbox:" << checked;
chart.dataModel()->setProperty("test_var", checked);
});
connect(ui->pushButton, &QPushButton::released, [this](){
qDebug() << "> button";
chart.submitEvent("t_button_clicked");
});
chart.start();
}
(testchart.scxml)
<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" name="TestChart" qt:editorversion="4.14.1" datamodel="ecmascript" initial="s_initial">
<qt:editorinfo initialGeometry="213.11;86.67;-20;-20;40;40"/>
<state id="s_initial">
<qt:editorinfo scenegeometry="213.11;233.50;153.11;183.50;120;100" geometry="213.11;233.50;-60;-50;120;100"/>
<transition type="external" event="t_button_clicked" target="s_false" cond="!test_var">
<qt:editorinfo endTargetFactors="11.79;50.87"/>
</transition>
<transition type="external" event="t_button_clicked" target="s_true" cond="test_var">
<qt:editorinfo movePoint="37.73;-3.06" endTargetFactors="19.26;54.39"/>
</transition>
<onentry>
<log expr=""s_initial""/>
</onentry>
</state>
<state id="s_false">
<qt:editorinfo scenegeometry="529.21;233.50;469.21;183.50;120;100" geometry="529.21;233.50;-60;-50;120;100"/>
<onentry>
<log expr=""s_false""/>
</onentry>
<transition type="external" event="t_button_clicked" target="s_initial">
<qt:editorinfo movePoint="3.06;9.18" endTargetFactors="88.28;64.08" startTargetFactors="13.98;61.45"/>
</transition>
</state>
<state id="s_true">
<qt:editorinfo scenegeometry="529.21;419.09;469.21;369.09;120;100" geometry="529.21;419.09;-60;-50;120;100"/>
<onentry>
<log expr=""s_true""/>
</onentry>
<transition type="external" event="t_button_clicked" target="s_initial">
<qt:editorinfo movePoint="-37.73;6.12" endTargetFactors="68.74;85.18" startTargetFactors="14.04;72.02"/>
</transition>
</state>
<datamodel>
<data id="test_var" expr="false"/>
</datamodel>
</scxml>
从 scxml 文件中可以看出,我将日志输出添加到每个 state-onentry 以查看该状态是否已进入。此外,我向按钮和复选框单击添加了调试输出。当我运行应用程序时,控制台输出不是我所期望的:
scxml.statemachine: "" : "s_initial"
> checkbox: false
> button
scxml.statemachine: "" : "s_false"
> button
scxml.statemachine: "" : "s_initial"
> checkbox: true
> button
scxml.statemachine: "" : "s_false"
> button
scxml.statemachine: "" : "s_initial"
> checkbox: false
> button
scxml.statemachine: "" : "s_false"
> button
scxml.statemachine: "" : "s_initial"
值是多少并不重要test_var
。状态机总是首先转换到s_false
,而不检查我添加的条件保护。据我所知,我在图表中使用了有效的 ecmascript 表达式,并且 scxml 应该能够根据其规范选择正确的转换。我究竟做错了什么?
解决方案
您应该使用setScxmlProperty而不是setProperty
chart.dataModel()->setScxmlProperty("test_var", checked, "");
您可以简单地使用_event.data来传递复选框当前值。
chart.submitEvent("t_button_clicked", ui->checkBox->checked() ? 1:0 );
<scxml datamodel="ecmascript" initial="s_initial" name="TestChart" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
<state id="s_initial">
<onentry>
<log expr="'s_initial'"/>
</onentry>
<transition cond="_event.data==1" event="t_button_clicked" target="s_true"/>
<transition event="t_button_clicked" target="s_false"/>
</state>
<state id="s_false">
<onentry>
<log expr="'s_false'"/>
</onentry>
<transition cond="_event.data==1" event="t_button_clicked" target="s_true"/>
</state>
<state id="s_true">
<onentry>
<log expr="'s_true'"/>
</onentry>
<transition cond="! (_event.data==1)" event="t_button_clicked" target="s_false"/>
</state>
</scxml>
PS你可以使用下一个参考资料更好地理解SCXML(我正在推广我自己的网站)
推荐阅读
- spring-boot - 我试图在请求参数中将 + 符号作为字符串传递,但它在处理时没有出现?
- apk - APK 文件创建
- reactjs - yarn build 失败 this.config.reduce 不是函数
- go - 连接中继器时如何解决余额不足错误?
- winforms - 将 Devexpress 项目从 v17.2 转换为 21.2(在 v21.2 中不再有 DevExpress.ExpressApp.Images.dll )
- spring - Bean 名称“产品”的 BindingResult 和普通目标对象都不能用作请求属性
- javascript - 数据未从前端传递到 REST API
- linux - 提取目录中文件数量最多的用户
- javascript - 将 CSS 注入 Selenium 驱动程序会导致 Linux 中的 javascript 错误,而不是 macOS
- python - 将图像放置在绘图图中