QCefView
|
QCefView provides the capabilities of interoperability between native context and web context, thus the developer can call Javascript from C++ code easily, vice versa. This allows you to create hybrid applications that combine the power of web technologies with the capabilities of native C++ code.
The approach of the interoperability was implemented by inserting a bridge object into the web context displayed in all the frames and browsers managed by QCefView. The bridge object provides methods for communicating with native code. For more details please refer to the API reference WebAPIs
QCefConfig::setBridgeObjectName
. The default name is CefViewClient.This section describes how to call C++ code from Javascript running within the QCefView. The bridge object provides methods to invoke C++ slots, allowing you to execute native code in response to events or actions in your web application.
The bridge object provides the following method to invoke C++ code from Javascript
CefViewClient.invoke(name, ...args)
When this method gets called from Javascript, the following Qt signal will be emitted
void QCefView::invokeMethod(int browserId,int frameId,const QString & method,const QVariantList & arguments)
(name, ...args)
is ASYNCHRONOUS
operation, that means the calling from Javascript returns immediately regardless the execution of C++ Qt slotNow let's write a small piece of code to demonstrate the invocation from Javascript to C++.
First add Javascript code as follows into the <script> block
and add the html code
Then add code in C++ to handle the invocation
Now let's run the application.
Click the button in web area to invoke the C++ code
This section explains how to call Javascript functions from C++ code. QCefView provides mechanisms to trigger events in the Javascript context, allowing you to update the UI or execute Javascript logic from your native application.
The bridge object provides the following methods to support calling from C++ code to Javascript
(name, listener)
(name, listener)
The developers can add as many event listeners as they want in the Javascript context and trigger the events from C++ code with the following methods
public bool QCefView::triggerEvent(const QCefEvent & event)
public bool QCefView::triggerEvent(const QCefEvent & event,int frameId)
public bool QCefView::broadcastEvent(const QCefEvent & event)
ASYNCHRONOUS
operationsNow let's code it
Add code to the javascript
Add code to the html
Add code to trigger the event be handled in Javascript, here we need to add a button in perform the triggering, we just show the button click action handler slot.
Now let's run the application.
Click the button in native area to invoke the Javascript code
In this section, we'll explore how to use CefViewQuery
to communicate asynchronously between JavaScript and C++ code in your CefView application. This method allows you to send requests from your web page to the native application and receive responses without blocking the user interface.
window.cefViewQuery(query)
is yet another approach to communicate from Javascript to C++ code, but in this way the communication is ASYNCHRONOUS
operation. For more details please refer to the API reference.
window.cefViewQuery
operates asynchronously. This means that when you send a query, your JavaScript code doesn't wait for the response. Instead, you provide callback functions (onSuccess
and onFailure
) that will be executed when the response arrives.When this method gets called from Javascript, the following Qt signal will be emitted:
public void QCefView::cefQueryRequest(int browserId,int frameId,const QCefQuery & query)
In this section let's demonstrate the usage of CefViewQuery with some simple code.
Add Javascript code as follows:
Add HTML code as follows:
Add C++ code as follows:
Now let's run the application.
Click the button in web area to invoke the C++ code