Revision control

- 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.
+ 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:
- document.write('<script src="' + (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/]+)/.exec(location.search)[1]) + '/webchannel.js/createWebChannel"><' + '/script>');
+ <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 {
- onExecute {
- if (requestData == "command") { response.send("..."); }
+ onRawMessageReceived {
+ if (requestData == "command") { sendRawMessage("..."); }
}
}
or subscribe to events:
HTML
webChannel.subscribe("myEvent", function(eventData) { ... });
QML
- webChannel.broadcast("myEvent", "...");
-
+ webChannel.sendMessage("myEvent", "...");
See examples for further details.