Calling C++ functions from JavaScript

Next, let's take a look at how we can call C++ functions from JavaScript instead. For the sake of demonstration, I'll put a text label above the web view and we will change its text using a JavaScript function:

Normally, JavaScript can only work within the HTML environment and hence, is only able to alter HTML elements and not something outside the web view. However, Qt allows us to do just that by using the web channel module. So let's open up our project (.pro) file and add the web channel module to the project:

QT += core gui webengine webenginewidgets webchannel 

After that, open up mainwindow.h and ...

Get Hands-On GUI Programming with C++ and Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.