QWebEngineFrame Class
The QWebEngineFrame class gives information about and control over a page frame. More...
| Header: | #include <QWebEngineFrame> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS WebEngineCore)target_link_libraries(mytarget PRIVATE Qt6::WebEngineCore) |
| qmake: | QT += webenginecore |
| Since: | Qt WebEngine 6.8 |
| In QML: | webEngineFrame |
Detailed Description
A web engine frame represents a single frame within a web page, such as those created by <frame> or <iframe> HTML elements. An active QWebEnginePage has one or more frames arranged in a tree structure. The top-level frame, the root of this tree, can be accessed through the mainFrame() method, and children() provides a frame's direct descendants.
A frame's lifetime is, at most, as long as the QWebEnginePage object that produced it. However, frames may be created and deleted spontaneously and dynamically, for example through navigation and script execution. Because of this, many QWebEngineFrame methods return optional values, which will be std::nullopt if the frame no longer exists.
Member Function Documentation
[private] template <typename Functor, int = true> void QWebEngineFrame::runJavaScript(const int &script, Functor &&callback)
[private] template <typename Functor, int = true> void QWebEngineFrame::runJavaScript(const int &script, int worldId, Functor &&callback)
Runs the JavaScript code contained in script on this frame, without checking whether the DOM of the page has been constructed. To avoid conflicts with other scripts executed on the page, the world in which the script is run is specified by worldId. The world ID values are the same as provided by QWebEngineScript::ScriptWorldId, and between 0 and 256. If you leave out the world ID, the script is run in the MainWorld. When the script has been executed, callback is called with the result of the last executed statement. callback can be any of a function pointer, a functor or a lambda, and it is expected to take a QVariant parameter. For example:
page.runJavaScript("document.title", [](const QVariant &v) { qDebug() << v.toString(); });
Only plain data can be returned from JavaScript as the result value. Supported data types include all of the JSON data types as well as, for example, Date and ArrayBuffer. Unsupported data types include, for example, Function and Promise.
Note: In Qt versions prior to 6.12, the callback had to be copyable.
Warning: Do not execute lengthy routines in the callback function, because it might block the rendering of the web engine page.
Warning: We guarantee that the callback is always called, but it might be done during page destruction. When QWebEnginePage is deleted, the callback is triggered with an invalid value and it is not safe to use the corresponding QWebEnginePage or QWebEngineView instance inside it.
See also QWebEngineScript::ScriptWorldId, QWebEnginePage::runJavaScript, and Script Injection.