blob: 43b8cfa91f3cc95448ee949936b8308b3ab67563 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
Qt Web Channel is a labs project that enables the creation of an HTTP-based channel between a web view and its QML/C++ container.
To build:
cd src
qmake
make
sudo make install
This would enable a QML import, which can be imported as such:
import Qt.labs.WebChannel 1.0
and then instantiated:
WebChannel {
id: webChannel
onExecute: {
}
}
A new WebChannel then generates a secret URL, that can be used from within an HTML page as a loaded script.
QML:
WebView {
id: webView
url: "index.html?webChannelBaseUrl=" + webChannel.baseUrl;
}
HTML:
document.write('<script src="' + (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/]+)/.exec(location.search)[1]) + '/webchannel.js/createWebChannel"><' + '/script>');
This would invoke a function named createWebChannel (or any other name passed in the URL), that would receieve as a parameter the web channel object.
That object can be used to execute commands:
HTML
webChannel.exec("command", function(result) { ... });
QML
WebChannel {
onExecute {
if (requestData == "command") { response.send("..."); }
}
}
or subscribe to events:
HTML
webChannel.subscribe("myEvent", function(eventData) { ... });
QML
webChannel.broadcast("myEvent", "...");
See examples for further details.
|