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 6.8

Public Functions

QList<QWebEngineFrame> children() const
QString htmlName() const
bool isMainFrame() const
bool isValid() const
QString name() const
void printToPdf(const QString &filePath)
void printToPdf(const std::function<void (const QByteArray &)> &callback)
void runJavaScript(const QString &script, const std::function<void (const QVariant &)> &callback)
void runJavaScript(const QString &script, quint32 worldId = 0)
void runJavaScript(const QString &script, quint32 worldId, const std::function<void (const QVariant &)> &callback)
QSizeF size() const
QUrl url() const
bool operator!=(const QWebEngineFrame &left, const QWebEngineFrame &right)
bool operator==(const QWebEngineFrame &left, const QWebEngineFrame &right)

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

void QWebEngineFrame::runJavaScript(const QString &script, const std::function<void (const QVariant &)> &callback)

[invokable] void QWebEngineFrame::runJavaScript(const QString &script, quint32 worldId = 0)

void QWebEngineFrame::runJavaScript(const QString &script, quint32 worldId, const std::function<void (const QVariant &)> &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.

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.

QList<QWebEngineFrame> QWebEngineFrame::children() const

Returns a list of the frame's children in an arbitrary order.

If the frame could not be found, returns an empty list.

QString QWebEngineFrame::htmlName() const

Returns the value of the frame's name HTML attribute, or an empty string if it has none.

If the frame could not be found, returns a null QString.

Note: Getter function for property htmlName.

See also name.

bool QWebEngineFrame::isMainFrame() const

Returns true if this object represents the page's main frame; false otherwise.

Note: Getter function for property isMainFrame.

bool QWebEngineFrame::isValid() const

Returns true if this object represents an existing frame; false otherwise.

Once a frame is invalid, it never becomes valid again.

Note: Getter function for property isValid.

QString QWebEngineFrame::name() const

Returns the frame name; that is, what would be returned by window.name in JavaScript.

If the frame could not be found, returns a null QString.

Note: Getter function for property name.

See also htmlName.

[invokable] void QWebEngineFrame::printToPdf(const QString &filePath)

Renders the current content of the frame into a PDF document and saves it in the location specified in filePath. Printing uses a page size of A4, portrait layout, and includes the full range of pages.

This method issues an asynchronous request for printing the web page into a PDF and returns immediately. To be informed about the result of the request, connect to the QWebEnginePage::pdfPrintingFinished() signal.

Note: The QWebEnginePage::Stop web action can be used to interrupt this asynchronous operation.

If a file already exists at the provided file path, it will be overwritten.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also QWebEnginePage::pdfPrintingFinished().

void QWebEngineFrame::printToPdf(const std::function<void (const QByteArray &)> &callback)

Renders the current content of the frame into a PDF document and returns a byte array containing the PDF data as parameter to callback. Printing uses a page size of A4, portrait layout, and includes the full range of pages.

The callback must take a const reference to a QByteArray as parameter. If printing was successful, this byte array will contain the PDF data, otherwise, the byte array will be empty.

Note: The QWebEnginePage::Stop web action can be used to interrupt this operation.

QSizeF QWebEngineFrame::size() const

Returns the size of the frame within the viewport.

If the frame could not be found, returns QSizeF().

Note: Getter function for property size.

QUrl QWebEngineFrame::url() const

Returns the URL of the content currently loaded in this frame.

If the frame could not be found, returns an empty QUrl.

Note: Getter function for property url.

Related Non-Members

[noexcept] bool operator!=(const QWebEngineFrame &left, const QWebEngineFrame &right)

Returns true if left and right represent different frames in the same web page, otherwise false.

[noexcept] bool operator==(const QWebEngineFrame &left, const QWebEngineFrame &right)

Returns true if left and right represent the same frame in the same web page, otherwise false.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.