QScxmlStateMachine Class | Qt SCXML 5.7

Qt 5.7 | Qt SCXML | C++ Classes | QScxmlStateMachine

Qt 5.7.1 Reference Documentation

# QScxmlStateMachine Class

The QScxmlStateMachine class provides an interface to the state machines created from SCXML files. More...

Header: | #include <QScxmlStateMachine>
qmake: | QT += scxml
Since: | Qt 5.7

- List of all members, including inherited members

## Public Types

enum | BindingMethod { EarlyBinding, LateBinding }

## Properties

- dataModel : QScxmlDataModel *
- initialValues : QVariantMap
- initialized : const bool
- running : bool

## Public Functions

QStringList | activeStateNames(bool compress = true) const
void | cancelDelayedEvent(const QString &sendId)
QMetaObject::Connection | connectToState(const QString &scxmlStateName, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
BindingMethod | dataBinding() const
QScxmlDataModel * | dataModel() const
QVariantMap | initialValues()
bool | isActive(const QString &scxmlStateName) const
bool | isDispatchableTarget(const QString &target) const
bool | isInitialized() const
bool | isInvoked() const
bool | isRunning() const
QString | name() const
QVector<QScxmlError> | parseErrors() const
QScxmlEventFilter * | scxmlEventFilter() const
QString | sessionId() const
void | setDataModel(QScxmlDataModel *model)
void | setInitialValues(const QVariantMap &initialValues)
void | setRunning(bool running)
void | setScxmlEventFilter(QScxmlEventFilter *newFilter)
void | setSessionId(const QString &id)
QStringList | stateNames(bool compress = true) const
void | submitEvent(QScxmlEvent *event)
void | submitEvent(const QString &eventName)
void | submitEvent(const QString &eventName, const QVariant &data)

## Public Slots

bool | init()
void | start()
void | stop()

## Signals

void | dataModelChanged(QScxmlDataModel *model)
void | eventOccurred(const QScxmlEvent &event)
void | externalEventOccurred(const QScxmlEvent &event)
void | finished()
void | initialValuesChanged(const QVariantMap &initialValues)
void | initializedChanged(bool initialized)
void | log(const QString &label, const QString &msg)
void | reachedStableState()
void | runningChanged(bool running)

## Static Public Members

QScxmlStateMachine * | fromData(QIODevice *data, const QString &fileName = QString())
QScxmlStateMachine * | fromFile(const QString &fileName)
QString | generateSessionId(const QString &prefix)

## Detailed Description

The QScxmlStateMachine class provides an interface to the state machines created from SCXML files.

QScxmlStateMachine is an implementation of the State Chart XML (SCXML).

All states that are defined in the SCXML file are accessible as properties of QScxmlStateMachine. These properties are boolean values and indicate whether the state is active or inactive.

All external signals defined inside the SCXML file that are of the `qt:signal` type, are accessible as signals of QScxmlStateMachine in the Qt mode. The only argument of these signals is always QVariant, which is of QMap<QString, QVariant> type containing the content of all the `<param>` elements specified as children of a `<send>` element. The name of each QScxmlStateMachine signal corresponds to the value defined in the event attribute of one `<send>` tag in the SCXML file.

## Member Type Documentation

### enum QScxmlStateMachine::BindingMethod

This enum specifies the binding method. The binding method controls the point in time when the initial values are assigned to the data elements.

Constant | Value | Description
--- | --- | ---
`QScxmlStateMachine::EarlyBinding` | `0` | All data elements are created and initialized at data-model initialization. This is the default.
`QScxmlStateMachine::LateBinding` | `1` | All data elements are created at initialization, but the initial values are assigned only when the containing state is entered for the first time. This is done before any executable content is executed.

## Property Documentation

### dataModel : QScxmlDataModel *

This property holds the data model to be used for this state machine.

SCXML data models are described in SCXML Specification - 5 Data Model and Data Manipulation. For more information about supported data models, see SCXML Compliance.

Changing the data model when the state machine has been `initialized` is not specified in the SCXML standard and leads to undefined behavior.

Access functions:

QScxmlDataModel * | dataModel() const
void | setDataModel(QScxmlDataModel *model)

Notifier signal:

void | dataModelChanged(QScxmlDataModel *model)

See also QScxmlDataModel, QScxmlNullDataModel, QScxmlEcmaScriptDataModel, and QScxmlCppDataModel.

### initialValues : QVariantMap

This property holds the initial values to be used for setting up the data model.

Access functions:

QVariantMap | initialValues()
void | setInitialValues(const QVariantMap &initialValues)

Notifier signal:

void | initialValuesChanged(const QVariantMap &initialValues)

See also QScxmlStateMachine::init() and QScxmlDataModel.

### initialized : const bool

This property holds whether the state machine has been initialized.

It is `true` if the state machine has been initialized, `false` otherwise.

Access functions:

bool | isInitialized() const

Notifier signal:

void | initializedChanged(bool initialized)

See also QScxmlStateMachine::init() and QScxmlDataModel.

### running : bool

This property holds the running state of this state machine.

Access functions:

bool | isRunning() const
void | setRunning(bool running)

Notifier signal:

void | runningChanged(bool running)

See also start().

## Member Function Documentation

### QStringList QScxmlStateMachine::activeStateNames(bool compress = true) const

Retrieves a list of state names of all active states.

When a state is active, all its parent states are active by definition. When compress is `true` (the default), the parent states will be filtered out and only the leaf states will be returned. When it is `false`, the full list of active states will be returned.

### void QScxmlStateMachine::cancelDelayedEvent(const QString &sendId)

Cancels a delayed event with the specified sendId.

### QMetaObject::Connection QScxmlStateMachine::connectToState(const QString &scxmlStateName, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

Creates a connection of the given type from the state identified by scxmlStateName to the method in the receiver object. The receiver's method may contain a boolean argument that indicates whether the state connected became active or inactive.

Returns a handle to the connection, which can be used later to disconnect.

### BindingMethod QScxmlStateMachine::dataBinding() const

Returns the binding method used by the state machine.

### QScxmlDataModel *QScxmlStateMachine::dataModel() const

Returns the data model used by the state machine.

Note: Getter function for property dataModel.

See also setDataModel().

### `[signal]` void QScxmlStateMachine::eventOccurred(const QScxmlEvent &event)

This signal is emitted when the SCXML event event occurs. This signal is emitted for all events.

See also externalEventOccurred().

### `[signal]` void QScxmlStateMachine::externalEventOccurred(const QScxmlEvent &event)

This signal is emitted for each `<send>` element in the SCXML file that contains the attribute `type="qt:signal"`. The event that occurred is specified by event.

See also eventOccurred().

### `[signal]` void QScxmlStateMachine::finished()

This signal is emitted when the state machine reaches a top-level final state.

See also running.

### `[static]` QScxmlStateMachine *QScxmlStateMachine::fromData(QIODevice *data, const QString &fileName = QString())

Creates a state machine by reading from the QIODevice specified by data.

This method will always return a state machine. If errors occur while reading the SCXML file, fileName, the state machine cannot be started. The errors can be retrieved by calling the parseErrors() method.

See also parseErrors().

### `[static]` QScxmlStateMachine *QScxmlStateMachine::fromFile(const QString &fileName)

Creates a state machine from the SCXML file specified by fileName.

This method will always return a state machine. If errors occur while reading the SCXML file, the state machine cannot be started. The errors can be retrieved by calling the parseErrors() method.

See also parseErrors().

### `[static]` QString QScxmlStateMachine::generateSessionId(const QString &prefix)

Generates a unique ID by appending a unique number to the prefix.

The number is only unique within a single run of an application. This method is used when an invoked service does not have an ID set (the id attribute in `<invoke>`).

### `[slot]` bool QScxmlStateMachine::init()

Initializes the state machine.

State machine initialization consists of calling QScxmlDataModel::setup(), setting the initial values for `<data>` elements, and executing any `<script>` tags of the `<scxml>` tag. The initial data values are taken from the `initialValues` property.

Returns `false` if parse errors occur or if any of the initialization steps fail. Returns `true` otherwise.

### bool QScxmlStateMachine::isActive(const QString &scxmlStateName) const

Returns `true` if the state specified by scxmlStateName is active, `false` otherwise.

### bool QScxmlStateMachine::isDispatchableTarget(const QString &target) const

Returns `true` if a message to target can be dispatched by this state machine.

Valid targets are:

- `#_parent` for the parent state machine if the current state machine is started by `<invoke>`
- `#_internal` for the current state machine
- `#_scxml_sessionid`, where `sessionid` is the session ID of the current state machine
- `#_servicename`, where `servicename` is the ID or name of a service started with `<invoke>` by this state machine

### bool QScxmlStateMachine::isInvoked() const

Returns `true` when the state machine was started as a service with the `<invoke>` element, `false` otherwise.

### bool QScxmlStateMachine::isRunning() const

Returns `true` if the state machine is running, `false` otherwise.

Note: Getter function for property running.

See also setRunning() and runningChanged().

### `[signal]` void QScxmlStateMachine::log(const QString &label, const QString &msg)

This signal is emitted if a `<log>` tag is used in the SCXML. label is the value of the label attribute of the `<log>` tag. msg is the value of the evaluated expr attribute of the `<log>` tag. If there is no expr attribute, a null string will be returned.

### QString QScxmlStateMachine::name() const

Returns the name of the state machine as set by the name attribute of the `<scxml>` tag.

### QVector<QScxmlError> QScxmlStateMachine::parseErrors() const

Returns the list of parse errors that occurred while creating a state machine from an SCXML file.

### `[signal]` void QScxmlStateMachine::reachedStableState()

This signal is emitted when the event queue is empty at the end of a macro step or when a final state is reached.

### `[signal]` void QScxmlStateMachine::runningChanged(bool running)

This signal is emitted when the `running` property is changed with running as argument.

Note: Notifier signal for property running.

### QScxmlEventFilter *QScxmlStateMachine::scxmlEventFilter() const

Returns the SCXML event filter if one is set, otherwise returns null.

See also setScxmlEventFilter().

### QString QScxmlStateMachine::sessionId() const

Returns the session ID for the current state machine.

The session ID is used for message routing between parent and child state machines. If a state machine is started by an `<invoke>` element, any event it sends will have the `invokeid` field set to the session ID. The state machine will use the origin of an event (which is set by the target or targetexpr attribute in a `<send>` element) to dispatch messages to the correct child state machine.

See also setSessionId() and QScxmlEvent::invokeId().

### void QScxmlStateMachine::setDataModel(QScxmlDataModel *model)

Sets the data model for this state machine to model. There is a 1:1 relation between state machines and models. After setting the model once you cannot change it anymore. Any further attempts to set the model using this method will be ignored.

Note: Setter function for property dataModel.

See also dataModel().

### void QScxmlStateMachine::setRunning(bool running)

Starts the state machine if running is `true`, or stops it otherwise.

Note: Setter function for property running.

See also start(), stop(), isRunning(), and runningChanged().

### void QScxmlStateMachine::setScxmlEventFilter(QScxmlEventFilter *newFilter)

Sets the newFilter as the SCXML event filter. Passing null will remove the current filter.

See also scxmlEventFilter().

### void QScxmlStateMachine::setSessionId(const QString &id)

Sets the session ID for the current state machine to id.

See also sessionId().

### `[slot]` void QScxmlStateMachine::start()

Starts this state machine. The machine will reset its configuration and transition to the initial state. When a final top-level state is entered, the machine will emit the finished() signal.

Note: A state machine will not run without a running event loop, such as the main application event loop started with QCoreApplication::exec() or QApplication::exec().

See also runningChanged(), setRunning(), stop(), and finished().

### QStringList QScxmlStateMachine::stateNames(bool compress = true) const

Retrieves a list of state names of all states.

When compress is `true` (the default), the states that contain child states will be filtered out and only the leaf states will be returned. When it is `false`, the full list of all states will be returned.

The returned list does not contain the states of possible nested state machines.

### `[slot]` void QScxmlStateMachine::stop()

Stops this state machine. The machine will not execute any further state transitions. Its `running` property is set to `false`.

See also runningChanged(), start(), and setRunning().

### void QScxmlStateMachine::submitEvent(QScxmlEvent *event)

Submits the SCXML event event to the internal or external event queue depending on the priority of the event.

When a delay is set, the event will be queued for delivery after the timeout has passed.

### void QScxmlStateMachine::submitEvent(const QString &eventName)

A utility method to create and submit an external event with the specified eventName as the name.

### void QScxmlStateMachine::submitEvent(const QString &eventName, const QVariant &data)

A utility method to create and submit an external event with the specified eventName as the name and data as the payload data.