Qt 6.2中的状态机

State Machines in Qt 6.2

Qt 6.2中的状态机

Thursday December 16, 2021 by Tamás Martinec | Comments

2021年12月16日星期四 Tamás Martinec |评论

State machines are abstract computational machines that can be in only one of their finite number of states at any given time. They can change from one state to another in response to inputs. State machines can be defined by the list of their states, their initial state and the inputs that trigger the state transitions. State machines are usually visualized by state charts:

状态机是一种抽象的计算机,在任何给定时间只能处于有限数量的状态之一。它可以根据输入从一种状态转换为另一种状态。状态机可以通过它的状态列表、初始状态和触发状态转换的输入来定义。状态机通常通过状态图可视化

Qt 6.2中的状态机_第1张图片

A state chart from the Sub-Attaq Qt State Machine Framework example. 

Qt状态机框架示例中的状态图。

Qt 5 has two components which support creating and working with state machines: the Qt State Machine Framework and Qt SCXML.

Qt 5有两个支持创建和使用状态机的组件:Qt状态机框架和Qt SCXML。

Qt State Machine Framework

Qt状态机框架

The Qt State Machine Framework provides classes for creating and executing state machines. States and transitions can be created and connected both programmatically and from QML. The framework integrates tightly with Qt’s meta-object system: transitions between states can be triggered by signals, and states can be configured to set properties and invoke methods on QObjects. The framework supports hierarchical states, parallel states, history states and it is also integrated with the Animation API in Qt to allow automatically animating properties when they are assigned to states. QStateMachines can also be nested to break down big state machines to smaller functional units for clarity or reuse.

Qt状态机框架提供了用于创建和执行状态机的类。状态和过渡可以通过编程和QML来创建和连接。该框架与Qt的元对象系统紧密集成:状态之间的过渡可以由信号触发,状态可以设置属性和调用方法。该框架支持分层状态、并行状态、历史状态,它还与Qt中的Animation API集成,允许属性被分配到指定状态时自动动画化。QStateMachines还可以嵌套,以便将大型状态机分解为更小的功能单元,以便清晰或重用

Qt SCXML

SCXML stands for State Chart XML. It is a standardized XML based markup language that is able to describe complex state machines. The Qt SCXML module creates classes from SCXML files that can be embedded in Qt applications. Parts of the application logic can be replaced with an encapsulated SCXML file. This enables creating a clear division between the application logic and the user interface implementation by using Qt Quick or Qt Widgets. In Qt SCXML, state machines are read from separate SCXML files and integrated to Qt applications by instantiating the QScxmlStateMachine class and loading an SCXML file dynamically or by using the Qt SCXML Compiler to generate a subclass of QScxmlStateMachine. The communication with the SCXML document is provided by the signals and methods of the QScxmlStateMachine class. Qt SCXML also integrates tightly with Qt's meta-object system for signal triggered state transitions and setting properties and invoking methods on QObjects by states.

SCXML代表状态图XML。它是一种标准化的基于XML的标记语言,能够描述复杂的状态机。Qt SCXML模块从SCXML文件创建类,这些类可以嵌入到Qt应用程序中。可以用封装的SCXML文件替换部分应用程序逻辑。这使得可以通过使用Qt Quick或Qt Widgets,分离应用程序逻辑和用户界面实现。在Qt SCXML中,可以从单独的SCXML文件中读取状态机,并将其集成到Qt应用程序中,如通过实例化QScxmlStateMachine类和动态加载SCXML文件,或者使用Qt SCXML编译器生成QScxmlStateMachine的一个子类。与SCXML文档的通信是由QScxmlStateMachine类的信号和方法提供的。Qt SCXML还与Qt的元对象系统紧密集成,用于信号触发状态过渡、设置属性和通过状态调用QObjects的方法。

The Qt State Machine Framework versus Qt SCXML

Qt状态机框架与Qt SCXML对比

With the Qt State Machine Framework each state and transition has to be created and connected from code. The functionality of these state machine components are highly customizable but the more complicated the state machine gets the harder it is to keep everything in mind for the developers. Creating simple state machines is fast and  doesn’t require any tools or knowledge of a specific file format.

使用Qt状态机框架,每个状态和过渡都必须从代码中创建和连接。这些状态机组件的功能是高度可定制的,但是状态机越复杂,开发者就越难记住所有的东西。创建简单的状态机非常快,不需要任何工具或特定文件格式的知识。

The SCXML files for Qt SCXML can be created with any suitable tool, such as a text editor or a visual editor (Qt Creator features one). This allows for creating and modifying very complex state machines effortlessly, and also making them easier to understand. On the other hand the resulting state machine components are less customizable. Also Qt SCXML performs better in many ways regarding execution speed and memory footprint compared to the Qt State Machine Framework.

Qt SCXML的SCXML文件可以用任何合适的工具创建,比如文本编辑器或可视编辑器(Qt Creator的一个功能)。这允许轻松地创建和修改非常复杂的状态机,也使它们更容易理解。另一方面,生成的状态机组件的可定制性较差。此外,与Qt状态机框架相比,Qt SCXML在执行速度和内存占用方面,多表现得更好。

Changes in Qt 6

Qt 6中的变动

In Qt 6 both state machine modules were retained with some small arrangements. Both modules were updated to support building with cmake and C++ property bindings. Generally speaking the state machine modules are source compatible with their Qt 5 versions and users of the library should be able to continue with zero or only minor changes to their projects.

在Qt 6中,两个状态机模块都保留了一些原来的接口。这两个模块都更新了,以支持cmake和c++属性绑定的构建。一般来说,状态机模块与它们的Qt 5版本是源代码兼容的,并且旧库的用户应该能够继续他们的项目,不需要做任何修改,或者只需要做很少的改动。

State Machine modules

状态机的模块

The Qt State Machine Framework was moved into its own Qt State Machines module and therefore is no longer part of Qt Core. There were very few cross dependencies inside Qt Core which ultimately led to this decision. It can be found via the general Qt module listing in the Qt documentation now. The Qt SCXML module continues to exist as an independent and supported module.

Qt状态机框架被移到它自己的Qt状态机模块中,因此不再是Qt核心的一部分。最终导致与Qt核心内部的交叉依赖关系非常少。现在可以通过Qt文档中的通用Qt模块列表找到它。Qt SCXML模块继续作为一个独立的、受支持的模块存在。

Ecmascript data model moved to a plugin

Ecmascript数据模型移到了一个插件中

SCXML gives authors the ability to define a first-class data model as part of the SCXML document. The data model offers the capability of storing, reading, and modifying a set of data that is internal to the state machine. The SCXML specification does not mandate any specific data model, but instead defines a set of abstract capabilities that can be realized by various languages, such as ECMAScript or XML/XPath. Qt SCXML supports the null data model, which must be supported by all conforming SCXML processors, and the ECMAScript data model. In addition, Qt SCXML provides its own C++ data model that is implemented by the QScxmlCppDataModel class. In Qt 5, the ECMAScript data model is linked to QML. In Qt 6, the ECMAScript data model was moved to a plugin which allows building of Qt SCXML without having to link to QML This change contributes to more flexible deploy options.

SCXML使创作者能够将一级数据模型定义为SCXML文档的一部分。数据模型提供了存储、读取和修改状态机内部数据集的功能。SCXML规范没有强制要求任何特定的数据模型,而是定义了一组可以由各种语言(如ECMAScript或XML/XPath)实现的抽象功能。Qt SCXML支持空数据模型(所有符合标准的SCXML处理器都必须支持该模型)和ECMAScript数据模型。此外,Qt SCXML提供了自己的c++数据模型,该模型是由QScxmlCppDataModel类实现的。在Qt 5中,ECMAScript数据模型被链接到QML。在Qt 6中,ECMAScript数据模型被移到了一个插件中,这样就可以在不链接到QML的情况下构建Qt SCXML。

Qt bindable properties

Qt的属性绑定

Qt 6 introduced the concept of bindable properties for QObject based properties. Consequently,   

the C++ bindable property feature was applied to the state machine modules, such that users of these modules can benefit from its advantages.

Qt 6为基于QObject的属性引入了可绑定属性的概念。因此,c++可绑定属性特性被应用到状态机模块中,这样这些模块的用户就可以受益于它的优点。

Bug fixes

漏洞修复

All high priority bugs and several less important bugs were fixed as part of porting the modules to Qt 6. The full list of changes can be found in the modules “Qt 6 changes” documentation (Changes to the Qt State Machine Framework, Changes to Qt SCXML).

在将模块移植到Qt 6的过程中,所有高优先级的bug和几个不太重要的bug都得到了修复。完整的变动列表可以在Qt 6变动文档模块中找到(Qt状态机框架变动,Qt SCXML变动)。

Suggestions

建议

We would be happy to hear your thoughts in the comments about these modules, their current state as you experienced them and ideas about how to improve them.   

 我们很乐意在评论中听到您对这些模块的想法、当前状态以及如何改进的想法。

你可能感兴趣的:(QtBlog,qt)