Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
Qt Web Utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Morten Sorvig
Qt Web Utils
Commits
7d93eacf
Commit
7d93eacf
authored
1 year ago
by
Morten Sorvig
Browse files
Options
Downloads
Patches
Plain Diff
Add JavaScript <-> C++ interop example
parent
ac8f28a3
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/javascript_interop/index.html
+45
-0
45 additions, 0 deletions
examples/javascript_interop/index.html
examples/testapp/CMakeLists.txt
+5
-1
5 additions, 1 deletion
examples/testapp/CMakeLists.txt
examples/testapp/jsinterop.cpp
+36
-0
36 additions, 0 deletions
examples/testapp/jsinterop.cpp
with
86 additions
and
1 deletion
examples/javascript_interop/index.html
0 → 100644
+
45
−
0
View file @
7d93eacf
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"utf-8"
>
<title>
JavaScript Interop
</title>
<script
src=
"../testapp/testapp.js"
></script>
<script
src=
"../testapp/qtloader.js"
></script>
<script>
// JavaScript function which can be called from C++, see
// ../testapp/jsinterop.cpp
function
helloJs
(
name
)
{
console
.
log
(
"
Hello
"
+
name
+
"
from helloJs
"
);
}
// A pice of data, can be accessed from C++ as well
window
.
data
=
new
Uint8Array
([
1
,
2
,
3
,
4
]);
window
.
addEventListener
(
'
load
'
,
async
()
=>
{
const
qtloader
=
QtLoader
({
containerElements
:
[
document
.
getElementById
(
"
container
"
)],
showCanvas
:
()
=>
{
// This example does provide a UI. Instead, enable the "Call C++"
// button which calls the function exported from jsinterop.cpp.
const
callCppButton
=
document
.
getElementById
(
"
callcpp
"
);
callCppButton
.
disabled
=
false
;
callCppButton
.
addEventListener
(
"
click
"
,
()
=>
{
qtloader
.
module
()[
"
helloCpp
"
](
"
Qt
"
);
});
},
});
qtloader
.
loadEmscriptenModule
(
"
../testapp/testapp
"
);
})
</script>
</head>
<body>
<h1>
Qt for WebAssembly: JavaScript Interop
</h1>
<p>
This examples show how to call C++ functions from JavaScript,
and vice versa. Output is printed to the JavaScript console.
</p>
<button
type=
"button"
disabled=
"true"
id=
"callcpp"
disabled
>
Call C++ function
</button>
<div
id=
"container"
style=
"width:320px; height:200px; visibility:hidden"
></div>
</body>
</html>
This diff is collapsed.
Click to expand it.
examples/testapp/CMakeLists.txt
+
5
−
1
View file @
7d93eacf
...
...
@@ -6,7 +6,11 @@ set(CMAKE_AUTOMOC ON)
find_package
(
Qt6 REQUIRED COMPONENTS Core Gui
)
qt_add_executable
(
testapp
main.cpp rasterwindow.cpp ../../qtwebutils.cpp popup.cpp
main.cpp
rasterwindow.cpp
popup.cpp
jsinterop.cpp
../../qtwebutils.cpp
)
target_link_libraries
(
testapp PUBLIC
...
...
This diff is collapsed.
Click to expand it.
examples/testapp/jsinterop.cpp
0 → 100644
+
36
−
0
View file @
7d93eacf
#include
<QtCore>
#include
<algorithm>
#include
<emscripten/bind.h>
#include
<emscripten/val.h>
// This example demonstrates how to interop with JavaScript code
// and data from C++ using Emscripten bind.h and val.h.
// Sample C++ function. This function is exported to JavaScript using
// EMSCRIPTEN_BINDINGS below.
void
helloCpp
(
std
::
string
name
)
{
qDebug
()
<<
"Hello"
<<
QString
::
fromStdString
(
name
)
<<
"from helloCpp"
;
// Call JS function. See defenition in ../examples/jsinterop/jsinterop.cpp
// The function is accessed via the global "window" object.
emscripten
::
val
window
=
emscripten
::
val
::
global
(
"window"
);
window
.
call
<
void
>
(
"helloJs"
,
name
);
// Copy data from a JavaScript Array. This copies from the
// JavaScript heap to the C++ heap.
QByteArray
data
=
QByteArray
::
fromEcmaUint8Array
(
window
[
"data"
]);
qDebug
()
<<
"Got data bytes"
<<
QString
::
number
(
data
[
0
])
<<
QString
::
number
(
data
[
1
])
<<
QString
::
number
(
data
[
2
])
<<
QString
::
number
(
data
[
3
]);
// Clear Js-side data, if we don't want to hold a copy there
window
.
set
(
"data"
,
emscripten
::
val
::
null
());
// Or assign back to it
std
::
reverse
(
data
.
begin
(),
data
.
end
());
window
.
set
(
"data"
,
data
.
toEcmaUint8Array
());
}
EMSCRIPTEN_BINDINGS
(
qt_webutils
)
{
emscripten
::
function
(
"helloCpp"
,
&
helloCpp
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment