webchannel-qml-example icon indicating copy to clipboard operation
webchannel-qml-example copied to clipboard

WebChannel QML Example

The Qt documentation doesn't seem to have an example using QML and WebChannel together, only C++ and WebSockets or other. So after painstakingly figuring this out myself through intense googling and trial and error, I have come up with this fairly minimal example of what I needed to do (which hopefully will save someone else some time), which was embed a local html file into a QML application and expose it to a C++ created object passed to my QML (and have two-way communication between the object and the html).

Here's a bunch of useful/less useful links which I encountered on my googling journey

Usage

Open the .pro file in QtCreator and hit Ctrl-R to run it. When you click the "Enlighten" button, you are invoking a function in a C++ object introduced to QML, which then in turn emits a signal which the embedded html receives and reacts to.

Notes

Another useful example would be something similar to this, but using WebSockets through QML instead of this qt.webChannelTransport, however, I couldn't figure out how to get that to work with my example.