blob: b8225f8587cca9aeba89a3d9a5db173b4a005589 (
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
55
56
57
58
59
60
|
Qt Web Channel is a labs project that enables the creation of a WebSocket 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:
<script type="text/javascript" src="qrc:///qwebchannel/webchannel.js"></script>
<script type="text/javascript" src="qrc:///qwebchannel/qobject.js"></script>
<script type="text/javascript">
var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
var webChannel = new QWebChannel(baseUrl, function(channel) {
// channel is connected, send data!
});
</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 {
onRawMessageReceived {
if (requestData == "command") { sendRawMessage("..."); }
}
}
or subscribe to events:
HTML
webChannel.subscribe("myEvent", function(eventData) { ... });
QML
webChannel.sendMessage("myEvent", "...");
See examples for further details.
|