aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2023-03-29 14:53:28 +0800
committerMitch Curtis <mitch.curtis@qt.io>2023-05-24 08:03:16 +0800
commitc504227e235885199717129e7c05addd728361fa (patch)
tree9c8dc0e735a18ac35dd5c0c9403f58c608a019ee /tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp
parentf2823f0a04b3c26daed51195af9a388810e30c02 (diff)
Doc: add how-to for using C++ enums in JavaScript
The best search result for "qt how do i expose a C++ enum to javascript" is currently a Qt Forum post, and the relevant information in our docs is hard to find within QJSEngine's page. Add a how-to to make it easier to find the answer. Task-number: QTBUG-109634 Pick-to: 6.5 Change-Id: Ia9c53c5d3826459655aa5b094c4d601b938ef3a3 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp')
-rw-r--r--tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp
new file mode 100644
index 0000000000..9062dbad3f
--- /dev/null
+++ b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp
@@ -0,0 +1,36 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+//! [file]
+#include "backend.h"
+
+Backend::Backend(QJSEngine *engine) :
+ mEngine(engine)
+{
+}
+
+bool Backend::load()
+{
+ // Do some loading here...
+
+ const QJSValue module = mEngine->importModule(":/script.mjs");
+ if (module.isError()) {
+ qWarning() << "Error loading script.mjs:" << module.toString();
+ return false;
+ }
+
+ const QJSValue function = module.property("backendStatusUpdate");
+ if (!function.isCallable()) {
+ qWarning() << "backendStatusUpdate script function is not callable!";
+ return false;
+ }
+
+ const QJSValue functionResult = function.call(QJSValueList() << Loaded);
+ if (functionResult.isError()) {
+ qWarning() << "backendStatusUpdate script function had errors:" << functionResult.toString();
+ return false;
+ }
+
+ return true;
+}
+//! [file]