diff options
author | Juha Vuolle <juha.vuolle@qt.io> | 2023-03-08 11:23:17 +0200 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@qt.io> | 2023-03-17 13:02:01 +0200 |
commit | 4046e88de0f090b59e26de0c86192d564e46b9d9 (patch) | |
tree | 679af528027e0335c3c79914087bb2a8bde48612 | |
parent | 81f50767f81e5d5105468f36b0fe9887c0993e9f (diff) |
Revamp invoke example
Pick-to: 6.5
Task-number: QTBUG-111323
Change-Id: If4549b346a824873753f512a4e3c7d4ccc2b1f88
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | examples/scxml/invoke/Button.qml | 4 | ||||
-rw-r--r-- | examples/scxml/invoke/CMakeLists.txt | 33 | ||||
-rw-r--r-- | examples/scxml/invoke/MainView.qml | 21 | ||||
-rw-r--r-- | examples/scxml/invoke/SubView.qml | 17 | ||||
-rw-r--r-- | examples/scxml/invoke/doc/src/invoke.qdoc | 17 | ||||
-rw-r--r-- | examples/scxml/invoke/invoke-qml.h | 20 | ||||
-rw-r--r-- | examples/scxml/invoke/invoke.cpp | 17 | ||||
-rw-r--r-- | examples/scxml/invoke/invoke.pro | 15 | ||||
-rw-r--r-- | examples/scxml/invoke/invoke.qml | 10 | ||||
-rw-r--r-- | examples/scxml/invoke/invoke.qrc | 8 | ||||
-rw-r--r-- | examples/scxml/invoke/qmldir | 5 | ||||
-rw-r--r-- | examples/scxml/invoke/statemachine.scxml | 4 |
12 files changed, 101 insertions, 70 deletions
diff --git a/examples/scxml/invoke/Button.qml b/examples/scxml/invoke/Button.qml index 0b7e6b2..5be5665 100644 --- a/examples/scxml/invoke/Button.qml +++ b/examples/scxml/invoke/Button.qml @@ -1,6 +1,8 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +pragma ComponentBehavior: Bound + import QtQuick Item { diff --git a/examples/scxml/invoke/CMakeLists.txt b/examples/scxml/invoke/CMakeLists.txt index 93bbf58..5511792 100644 --- a/examples/scxml/invoke/CMakeLists.txt +++ b/examples/scxml/invoke/CMakeLists.txt @@ -1,11 +1,9 @@ -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(invoke LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() @@ -14,6 +12,8 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/scxml/invoke") find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml) +qt_standard_project_setup(REQUIRES 6.5) + qt_add_executable(invoke invoke.cpp ) @@ -23,32 +23,29 @@ set_target_properties(invoke PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(invoke PUBLIC +target_link_libraries(invoke PRIVATE Qt6::Core Qt6::Gui Qt6::Qml Qt6::Scxml ) -set(invoke_resource_files - "Button.qml" - "MainView.qml" - "SubView.qml" - "invoke.qml" -) - -qt6_add_resources(invoke "invoke" - PREFIX - "/" - FILES - ${invoke_resource_files} -) - # Statecharts: qt6_add_statecharts(invoke statemachine.scxml ) +qt_add_qml_module(invoke + URI InvokeExample + VERSION 1.0 + QML_FILES + Button.qml + MainView.qml + SubView.qml + SOURCES + invoke-qml.h +) + install(TARGETS invoke RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/scxml/invoke/MainView.qml b/examples/scxml/invoke/MainView.qml index 6ae5155..c7a4400 100644 --- a/examples/scxml/invoke/MainView.qml +++ b/examples/scxml/invoke/MainView.qml @@ -1,19 +1,25 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +pragma ComponentBehavior: Bound + import QtQuick import QtQuick.Window import QtScxml +import InvokeExample Window { id: window visible: true - property StateMachine stateMachine - color: "black" width: 400 height: 300 + DirectionsStateMachine { + id: stateMachine + running: true + } + Item { width: parent.width / 2 height: parent.height @@ -39,7 +45,9 @@ Window { } Loader { - source: "SubView.qml" + sourceComponent: SubView { + anywhere: services.children.anywhere ? services.children.anywhere.stateMachine : null + } active: stateMachine.somewhere x: parent.width / 2 @@ -48,11 +56,8 @@ Window { InvokedServices { id: services - stateMachine: window.stateMachine + stateMachine: stateMachine } - - property var anywhere: services.children.anywhere ? services.children.anywhere.stateMachine - : undefined } } diff --git a/examples/scxml/invoke/SubView.qml b/examples/scxml/invoke/SubView.qml index 413227b..31dbf06 100644 --- a/examples/scxml/invoke/SubView.qml +++ b/examples/scxml/invoke/SubView.qml @@ -1,26 +1,31 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +pragma ComponentBehavior: Bound + import QtQuick +import QtScxml Item { - // "anywhere" is a context property, so we always have to check if it's null + id: root + required property StateMachine anywhere + Button { id: here - enabled: anywhere ? anywhere.here : false + enabled: root.anywhere ? root.anywhere.here : false text: "Go There" width: parent.width / 2 height: parent.height - onClicked: anywhere.submitEvent("goThere") + onClicked: root.anywhere.submitEvent("goThere") } Button { id: there - enabled: anywhere ? anywhere.there : false + enabled: root.anywhere ? root.anywhere.there : false text: "Go Here" width: parent.width / 2 height: parent.height x: width - onClicked: anywhere.submitEvent("goHere") + onClicked: root.anywhere.submitEvent("goHere") } } diff --git a/examples/scxml/invoke/doc/src/invoke.qdoc b/examples/scxml/invoke/doc/src/invoke.qdoc index b771c3d..10b5aa2 100644 --- a/examples/scxml/invoke/doc/src/invoke.qdoc +++ b/examples/scxml/invoke/doc/src/invoke.qdoc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! @@ -20,7 +20,7 @@ \section1 Invoking the State Machine In \e statemachine.scxml, we specify a state machine with the name - \e Directions of type \e http://www.w3.org/TR/scxml/ to invoke: + \e DirectionsStateMachine of type \e http://www.w3.org/TR/scxml/ to invoke: \quotefromfile invoke/statemachine.scxml \skipto scxml @@ -56,12 +56,19 @@ \e statemachine.cpp, which are then added appropriately as headers and sources for compilation. + \section1 Declaring the state machine as QML element + + The state machine is declared as a QML element as follows: + \quotefromfile invoke/invoke-qml.h + \skipto struct + \printuntil } + \section1 Instantiating the State Machine - We instantiate the generated \c Directions class in the \e invoke.cpp + We instantiate the generated \c DirectionsStateMachine element in the \e MainView.qml file, as follows: - \quotefromfile invoke/invoke.cpp - \skipto statemachine.h + \quotefromfile invoke/MainView.qml + \skipto DirectionsStateMachine \printuntil } */ diff --git a/examples/scxml/invoke/invoke-qml.h b/examples/scxml/invoke/invoke-qml.h new file mode 100644 index 0000000..e0735a8 --- /dev/null +++ b/examples/scxml/invoke/invoke-qml.h @@ -0,0 +1,20 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef INVOKE_QML +#define INVOKE_QML + +#include "statemachine.h" + +#include <QtQml/qqml.h> +#include <QtCore/qobject.h> + +struct DirectionsStateMachineRegistration +{ + Q_GADGET + QML_FOREIGN(DirectionsStateMachine) + QML_NAMED_ELEMENT(DirectionsStateMachine) + QML_ADDED_IN_VERSION(1, 0) +}; + +#endif // INVOKE_QML diff --git a/examples/scxml/invoke/invoke.cpp b/examples/scxml/invoke/invoke.cpp index 9a64243..6680396 100644 --- a/examples/scxml/invoke/invoke.cpp +++ b/examples/scxml/invoke/invoke.cpp @@ -1,22 +1,17 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QGuiApplication> -#include <QQmlApplicationEngine> -#include <QQmlContext> - -#include "statemachine.h" +#include <QtGui/qguiapplication.h> +#include <QtQml/qqmlapplicationengine.h> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - qmlRegisterType<Directions>("Directions", 1, 0, "Directions"); - QQmlApplicationEngine engine; - engine.load(QUrl(QStringLiteral("qrc:/invoke.qml"))); - if (engine.rootObjects().isEmpty()) - return -1; + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, + [](){ QCoreApplication::exit(EXIT_FAILURE); }, Qt::QueuedConnection); + engine.loadFromModule("InvokeExample", "MainView"); return app.exec(); } diff --git a/examples/scxml/invoke/invoke.pro b/examples/scxml/invoke/invoke.pro index 866ecdb..d23a0af 100644 --- a/examples/scxml/invoke/invoke.pro +++ b/examples/scxml/invoke/invoke.pro @@ -2,10 +2,23 @@ TEMPLATE = app QT += qml scxml CONFIG += c++11 +CONFIG += qmltypes SOURCES += invoke.cpp +HEADERS += invoke-qml.h -RESOURCES += invoke.qrc +QML_IMPORT_NAME = InvokeExample +QML_IMPORT_MAJOR_VERSION = 1 + +qml_resources.files = \ + qmldir \ + Button.qml \ + MainView.qml \ + SubView.qml + +qml_resources.prefix = /qt/qml/InvokeExample + +RESOURCES += qml_resources STATECHARTS = statemachine.scxml diff --git a/examples/scxml/invoke/invoke.qml b/examples/scxml/invoke/invoke.qml deleted file mode 100644 index e28a6f2..0000000 --- a/examples/scxml/invoke/invoke.qml +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import Directions 1.0 - -MainView { - stateMachine: Directions { - running: true - } -} diff --git a/examples/scxml/invoke/invoke.qrc b/examples/scxml/invoke/invoke.qrc deleted file mode 100644 index 24c0e89..0000000 --- a/examples/scxml/invoke/invoke.qrc +++ /dev/null @@ -1,8 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>invoke.qml</file> - <file alias="MainView.qml">MainView.qml</file> - <file alias="SubView.qml">SubView.qml</file> - <file alias="Button.qml">Button.qml</file> - </qresource> -</RCC> diff --git a/examples/scxml/invoke/qmldir b/examples/scxml/invoke/qmldir new file mode 100644 index 0000000..6816682 --- /dev/null +++ b/examples/scxml/invoke/qmldir @@ -0,0 +1,5 @@ +module InvokeExample +prefer :/qt/qml/InvokeExample/ +Button 1.0 Button.qml +MainView 1.0 MainView.qml +SubView 1.0 SubView.qml diff --git a/examples/scxml/invoke/statemachine.scxml b/examples/scxml/invoke/statemachine.scxml index 6a0f5a9..bd31dca 100644 --- a/examples/scxml/invoke/statemachine.scxml +++ b/examples/scxml/invoke/statemachine.scxml @@ -1,12 +1,12 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause --> <scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" - name="Directions" + name="DirectionsStateMachine" initial="anyplace" > <state id="anyplace"> |