SCXML 미디어 플레이어

C++ 데이터 모델과 데이터를 주고받습니다.

미디어 플레이어는 C++ 데이터 모델에서 데이터에 액세스하는 방법을 보여줍니다. 데이터 모델을 사용하면 expr 속성 및 <script> 요소에 대한 C++ 코드를 작성할 수 있습니다. 데이터 모델의 데이터 부분은 QScxmlCppDataModel 의 서브클래스에 의해 지원되며, Qt SCXML 컴파일러(qscxmlc)가 디스패치 메서드를 생성합니다.

UI는 Qt Quick 를 사용하여 생성됩니다.

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 예제를 실행하려면 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.

C++ 데이터 모델 사용

데이터 모델은 SCXML 파일에서 <scxml> 요소의 데이터모델 속성 값으로 지정합니다:

<scxml
    xmlns="http://www.w3.org/2005/07/scxml"
    version="1.0"
    name="MediaPlayerStateMachine"
    initial="stopped"
    datamodel="cplusplus:TheDataModel:thedatamodel.h"

데이터 모델 속성의 형식은 cplusplus:<class-name>:<classdef-header> 입니다. 따라서 QScxmlCppDataModel 의 서브클래스가 포함된 thedatamodel.h 라는 파일이 필요합니다:

#include "qscxmlcppdatamodel.h"
#include <QtQml/qqml.h>

class TheDataModel: public QScxmlCppDataModel
{
    Q_OBJECT
    Q_SCXML_DATAMODEL

QScxmlCppDataModelQObject 에서 파생되므로 정의의 비공개 섹션, 대괄호 바로 뒤에 Q_OBJECT 매크로를 추가합니다. 그런 다음 Q_OBJECT 뒤에 Q_SCXML_DATAMODEL 매크로를 배치합니다. 이 매크로는 가상 메서드의 선언으로 확장되며, 그 구현은 Qt SCXML 컴파일러에 의해 생성됩니다.

SCXML 파일에서는 <script> 요소에 C++ 문을 지정하고 expr 속성을 사용하여 데이터 모델에 액세스합니다:

    <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>

        <onexit>
            <send event="playbackStopped">
                <param name="media" expr="media"/>
            </send>
        </onexit>

        <transition event="tap" cond="!isValidMedia() || media == eventData().value(QStringLiteral(&quot;media&quot;))" target="stopped"/>
        <transition event="tap" cond="isValidMedia() &amp;&amp; media != eventData().value(QStringLiteral(&quot;media&quot;))" target="playing"/>
    </state>

Qt SCXML 컴파일러는 다양한 evaluateTo 메서드를 생성하고 해당 메서드 내부의 표현식 및 스크립트를 mediaplayer.cpp에서 람다로 변환합니다:

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(); }();
....
}

예제 프로젝트 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.