summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@qt.io>2023-03-06 14:16:36 +0200
committerJuha Vuolle <juha.vuolle@qt.io>2023-03-17 13:01:57 +0200
commit81f50767f81e5d5105468f36b0fe9887c0993e9f (patch)
tree94b1a4edfc858e0d20993819e0af3bc0ef087156
parentcd5967540d4fc3a8e4196cda3f89ad92ae08d1ad (diff)
Revamp calculatorscxml example
Pick-to: 6.5 Task-number: QTBUG-111323 Change-Id: I5f39ab281edde826d50c133a9d6a6d7fe9d85baa Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r--examples/scxml/calculator/Button.qml4
-rw-r--r--examples/scxml/calculator/CMakeLists.txt31
-rw-r--r--examples/scxml/calculator/MainWindow.qml (renamed from examples/scxml/calculator/calculator.qml)10
-rw-r--r--examples/scxml/calculator/calculator-qml.h20
-rw-r--r--examples/scxml/calculator/calculator.cpp18
-rw-r--r--examples/scxml/calculator/calculator.pro15
-rw-r--r--examples/scxml/calculator/calculator.qrc6
-rw-r--r--examples/scxml/calculator/doc/src/calculator.qdoc15
-rw-r--r--examples/scxml/calculator/qmldir5
9 files changed, 74 insertions, 50 deletions
diff --git a/examples/scxml/calculator/Button.qml b/examples/scxml/calculator/Button.qml
index edbedba..b2f0a79 100644
--- a/examples/scxml/calculator/Button.qml
+++ b/examples/scxml/calculator/Button.qml
@@ -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 BSD-3-Clause
import QtQuick
@@ -22,7 +22,7 @@ Rectangle {
height: parent.textHeight
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
- font.pixelSize: height * fontHeight
+ font.pixelSize: height * button.fontHeight
color: "#1b1c1d"
font.family: "Open Sans Regular"
}
diff --git a/examples/scxml/calculator/CMakeLists.txt b/examples/scxml/calculator/CMakeLists.txt
index 36e1b55..95b5d3e 100644
--- a/examples/scxml/calculator/CMakeLists.txt
+++ b/examples/scxml/calculator/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(calculator 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/calculator")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
+qt_standard_project_setup(REQUIRES 6.5)
+
qt_add_executable(calculatorscxml
calculator.cpp
)
@@ -23,31 +23,28 @@ set_target_properties(calculatorscxml PROPERTIES
MACOSX_BUNDLE TRUE
)
-target_link_libraries(calculatorscxml PUBLIC
+target_link_libraries(calculatorscxml PRIVATE
Qt::Core
Qt::Gui
Qt::Qml
Qt::Scxml
)
-# Resources:
-set(calculator_resource_files
- "Button.qml"
- "calculator.qml"
-)
-
-qt6_add_resources(calculatorscxml "calculator"
- PREFIX
- "/"
- FILES
- ${calculator_resource_files}
-)
-
# Statecharts:
qt6_add_statecharts(calculatorscxml
statemachine.scxml
)
+qt_add_qml_module(calculatorscxml
+ URI Calculator
+ VERSION 1.0
+ QML_FILES
+ MainWindow.qml
+ Button.qml
+ SOURCES
+ calculator-qml.h
+)
+
install(TARGETS calculatorscxml
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
diff --git a/examples/scxml/calculator/calculator.qml b/examples/scxml/calculator/MainWindow.qml
index 1f6849c..61a349d 100644
--- a/examples/scxml/calculator/calculator.qml
+++ b/examples/scxml/calculator/MainWindow.qml
@@ -1,10 +1,12 @@
-// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-import CalculatorStateMachine
+pragma ComponentBehavior: Bound
+
import QtQuick
import QtQuick.Window
import QtScxml
+import Calculator
Window {
id: window
@@ -64,6 +66,8 @@ Window {
id: operations
model: ["÷", "×", "+", "-"]
Button {
+ required property int index
+ required property string modelData
y: 0
x: index * width
width: parent.width / 4
@@ -87,6 +91,8 @@ Window {
id: digits
model: ["7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "C"]
Button {
+ required property int index
+ required property string modelData
x: (index % 3) * width
y: Math.floor(index / 3 + 1) * height
width: parent.width / 4
diff --git a/examples/scxml/calculator/calculator-qml.h b/examples/scxml/calculator/calculator-qml.h
new file mode 100644
index 0000000..92a574c
--- /dev/null
+++ b/examples/scxml/calculator/calculator-qml.h
@@ -0,0 +1,20 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef CALCULATOR_QML
+#define CALCULATOR_QML
+
+#include "statemachine.h"
+
+#include <QtQml/qqml.h>
+#include <QtCore/qobject.h>
+
+struct CalculatorStateMachineRegistration
+{
+ Q_GADGET
+ QML_FOREIGN(CalculatorStateMachine)
+ QML_NAMED_ELEMENT(CalculatorStateMachine)
+ QML_ADDED_IN_VERSION(1, 0)
+};
+
+#endif // CALCULATOR_QML
diff --git a/examples/scxml/calculator/calculator.cpp b/examples/scxml/calculator/calculator.cpp
index 3bf9265..301b7c2 100644
--- a/examples/scxml/calculator/calculator.cpp
+++ b/examples/scxml/calculator/calculator.cpp
@@ -1,23 +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<CalculatorStateMachine>("CalculatorStateMachine", 1, 0,
- "CalculatorStateMachine");
-
QQmlApplicationEngine engine;
- engine.load(QUrl(QStringLiteral("qrc:/calculator.qml")));
- if (engine.rootObjects().isEmpty())
- return -1;
+ QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app,
+ [](){ QCoreApplication::exit(EXIT_FAILURE); }, Qt::QueuedConnection);
+ engine.loadFromModule("Calculator", "MainWindow");
return app.exec();
}
diff --git a/examples/scxml/calculator/calculator.pro b/examples/scxml/calculator/calculator.pro
index a60a1d4..ba7d343 100644
--- a/examples/scxml/calculator/calculator.pro
+++ b/examples/scxml/calculator/calculator.pro
@@ -1,10 +1,23 @@
QT += qml scxml
CONFIG += c++11
+CONFIG += qmltypes
SOURCES += calculator.cpp
-RESOURCES += calculator.qrc
+HEADERS += calculator-qml.h
+
+QML_IMPORT_NAME = Calculator
+QML_IMPORT_MAJOR_VERSION = 1
+
+qml_resources.files = \
+ qmldir \
+ MainWindow.qml \
+ Button.qml
+
+qml_resources.prefix = /qt/qml/Calculator
+
+RESOURCES += qml_resources
STATECHARTS = statemachine.scxml
diff --git a/examples/scxml/calculator/calculator.qrc b/examples/scxml/calculator/calculator.qrc
deleted file mode 100644
index d749b23..0000000
--- a/examples/scxml/calculator/calculator.qrc
+++ /dev/null
@@ -1,6 +0,0 @@
-<RCC>
- <qresource prefix="/">
- <file>calculator.qml</file>
- <file>Button.qml</file>
- </qresource>
-</RCC>
diff --git a/examples/scxml/calculator/doc/src/calculator.qdoc b/examples/scxml/calculator/doc/src/calculator.qdoc
index 5399eb9..98e1926 100644
--- a/examples/scxml/calculator/doc/src/calculator.qdoc
+++ b/examples/scxml/calculator/doc/src/calculator.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
/*!
@@ -24,21 +24,16 @@
\section1 Instantiating the State Machine
We make the generated \c CalculatorStateMachine class available to QML by
- registering it as a QML type in the \e calculator.cpp file:
+ declaring it as a QML type in the \e calculator-qml.h file:
- \quotefromfile calculator/calculator.cpp
- \skipto statemachine.h
+ \quotefromfile calculator/calculator-qml.h
+ \skipto struct
\printuntil }
- To use the CalculatorStateMachine type in a QML file, we import it:
-
- \quotefromfile calculator/calculator.qml
- \skipto CalculatorStateMachine
- \printline CalculatorStateMachine
-
We instantiate a CalculatorStateMachine and listen to the \c updateDisplay
event. When it occurs, we change the text on the calculator display:
+ \quotefromfile calculator/MainWindow.qml
\skipto CalculatorStateMachine {
\printuntil /^ {4}\}/
diff --git a/examples/scxml/calculator/qmldir b/examples/scxml/calculator/qmldir
new file mode 100644
index 0000000..1782b57
--- /dev/null
+++ b/examples/scxml/calculator/qmldir
@@ -0,0 +1,5 @@
+module Calculator
+prefer :/qt/qml/Calculator/
+MainWindow 1.0 MainWindow.qml
+Button 1.0 Button.qml
+