QScxmlCppDataModel Class

The QScxmlCppDataModel class is a C++ data model for a Qt SCXML state machine. 更多...

头: #include <QScxmlCppDataModel>
qmake: QT += scxml
Since: Qt 5.7
继承: QScxmlDataModel

公共函数

QScxmlCppDataModel (QObject * parent = nullptr)
bool inState (const QString & stateName ) const
const QScxmlEvent & scxmlEvent () const

重实现公共函数

virtual void evaluateAssignment (QScxmlExecutableContent::EvaluatorId id , bool * ok )
virtual void evaluateForeach (QScxmlExecutableContent::EvaluatorId id , bool * ok , ForeachLoopBody * body )
virtual void evaluateInitialization (QScxmlExecutableContent::EvaluatorId id , bool * ok )
virtual bool hasScxmlProperty (const QString & name ) const
virtual QVariant scxmlProperty (const QString & name ) const
virtual void setScxmlEvent (const QScxmlEvent & event )
virtual bool setScxmlProperty (const QString & name , const QVariant & value , const QString & context )
virtual bool setup (const QVariantMap & initialDataValues )

额外继承成员

详细描述

The QScxmlCppDataModel class is a C++ data model for a Qt SCXML state machine.

The C++ data model for SCXML lets you write C++ code for expr 属性和 <script> 元素。 数据部分 的数据模型后援是子类化的 QScxmlCppDataModel , for which the Qt SCXML compiler ( qscxmlc ) will generate the dispatch methods. It cannot be used when loading an SCXML file at runtime.

Usage is through the datamodel 属性在 <scxml> 元素:

<scxml datamodel="cplusplus:TheDataModel:thedatamodel.h"  ....>
					

The format of the datamodel attribute is: cplusplus:<class-name>:<classdef-header> . So, for the example above, there should be a file thedatamodel.h containing a subclass of QScxmlCppDataModel , containing at least the following:

#include "qscxmlcppdatamodel.h"
class TheDataModel: public QScxmlCppDataModel
{
    Q_OBJECT
    Q_SCXML_DATAMODEL
};
					

The Q_SCXML_DATAMODEL has to appear in the private section of the class definition, for example right after the opening bracket, or after a Q_OBJECT macro. This macro expands to the declaration of some virtual methods whose implementation is generated by the Qt SCXML compiler.

The Qt SCXML compiler will generate the various evaluateTo methods, and convert expressions and scripts into lambdas inside those methods. For example:

<scxml datamodel="cplusplus:TheDataModel:thedatamodel.h" xmlns="http://www.w3.org/2005/07/scxml" version="1.0" name="MediaPlayerStateMachine">
    <state id="stopped">
        <transition event="tap" cond="isValidMedia()" target="playing"/>
    </state>
    <state id="playing">
        <onentry>
            <script>
                media = eventData().value(QStringLiteral(&quot;media&quot;)).toString();
            </script>
            <send event="playbackStarted">
                <param name="media" expr="media"/>
            </send>
        </onentry>
    </state>
</scxml>
					

This will result in:

bool TheDataModel::evaluateToBool(QScxmlExecutableContent::EvaluatorId id, bool *ok) {
    // ....
        return [this]()->bool{ return isValidMedia(); }();
    // ....
}
QVariant TheDataModel::evaluateToVariant(QScxmlExecutableContent::EvaluatorId id, bool *ok) {
    // ....
        return [this]()->QVariant{ return media; }();
    // ....
}
void TheDataModel::evaluateToVoid(QScxmlExecutableContent::EvaluatorId id, bool *ok) {
    // ....
        [this]()->void{ media = eventData().value(QStringLiteral("media")).toString(); }();
    // ....
}
					

So, you are not limited to call functions. In a <script> element you can put zero or more C++ statements, and in cond or expr attributes you can use any C++ expression that can be converted to the respective bool or QVariant . And, as the this pointer is also captured, you can call or access the data model (the media attribute in the example above). For the full example, see Qt SCXML: Media Player QML Example (C++ Data Model) .

另请参阅 QScxmlStateMachine and QScxmlDataModel .

成员函数文档编制

QScxmlCppDataModel:: QScxmlCppDataModel ( QObject * parent = nullptr)

Creates a new C++ data model with the parent object parent .

[虚拟] void QScxmlCppDataModel:: evaluateAssignment ( QScxmlExecutableContent::EvaluatorId id , bool * ok )

重实现自 QScxmlDataModel::evaluateAssignment ().

This method does not perform any action, ignores id , and sets ok to false . Override it in your specific data model in order to implement <assign> .

[虚拟] void QScxmlCppDataModel:: evaluateForeach ( QScxmlExecutableContent::EvaluatorId id , bool * ok , ForeachLoopBody * body )

重实现自 QScxmlDataModel::evaluateForeach ().

This method does not perform any action, ignores id and body , and sets ok to false . Override it in your specific data model in order to implement <foreach> .

[虚拟] void QScxmlCppDataModel:: evaluateInitialization ( QScxmlExecutableContent::EvaluatorId id , bool * ok )

重实现自 QScxmlDataModel::evaluateInitialization ().

This method does not perform any action, ignores id , and sets ok to false . Override it in your specific data model in order to implement <data> .

[虚拟] bool QScxmlCppDataModel:: hasScxmlProperty (const QString & name ) const

重实现自 QScxmlDataModel::hasScxmlProperty ().

This method always returns false and ignores name . Override it to implement the lookup of data model properties via the location attribute of various elements.

bool QScxmlCppDataModel:: inState (const QString & stateName ) const

返回 true if the state machine is in the state specified by stateName , false 否则。

const QScxmlEvent &QScxmlCppDataModel:: scxmlEvent () const

Holds the current event that is being processed by the state machine.

另请参阅 SCXML Specification - 5.10 System Variables for the description of the _event 变量。

Returns the event currently being processed.

另请参阅 setScxmlEvent ().

[虚拟] QVariant QScxmlCppDataModel:: scxmlProperty (const QString & name ) const

重实现自 QScxmlDataModel::scxmlProperty ().

This method always returns an empty QVariant and ignores name . Override it to implement the lookup of data model properties via the location attribute of various elements.

另请参阅 setScxmlProperty ().

[虚拟] void QScxmlCppDataModel:: setScxmlEvent (const QScxmlEvent & event )

重实现自 QScxmlDataModel::setScxmlEvent ().

设置 event that will be processed next.

另请参阅 QScxmlCppDataModel::scxmlEvent .

[虚拟] bool QScxmlCppDataModel:: setScxmlProperty (const QString & name , const QVariant & value , const QString & context )

重实现自 QScxmlDataModel::setScxmlProperty ().

This method always returns false and ignores name , value ,和 context . Override it to implement the lookup of data model properties via the location attribute of various elements.

另请参阅 scxmlProperty ().

[虚拟] bool QScxmlCppDataModel:: setup (const QVariantMap & initialDataValues )

重实现自 QScxmlDataModel::setup ().

Called during state machine initialization to set up a state machine using the initial values for data model variables specified by their keys, initialDataValues . These are the values specified by <param> tags in an <invoke> 元素。

另请参阅 QScxmlStateMachine::init .