Class: QgsWebEnginePage

A wrapper around the QWebEnginePage class, adding extra functionality.

Warning

This class is only available on QGIS builds with WebEngine support enabled.

Added in version 3.36.

List of all members, including inherited members

Class Hierarchy

Inheritance diagram of qgis.core.QgsWebEnginePage

Base classes

QObject

Constructor

__init__

Methods

documentSize

Returns the size of the page document, in pixels.

render

Renders the web page contents to a painter.

setContent

Sets the content of the web page to data.

setHtml

Sets the content of this page to html.

setUrl

Sets the url of the web page to be displayed.

Static Methods

printToPdfBlocking

Renders HTML content to a PDF file asynchronously.

renderBlocking

Renders HTML content to a painter asynchronously.

Signals

loadFinished

This signal is emitted when the page finishes loading content.

loadProgress

This signal is emitted when the global progress status changes.

loadStarted

This signal is emitted when the page starts loading content.

class qgis.core.QgsWebEnginePage[source]

Bases: QObject

__init__(parent: QObject | None = None)

Constructor for QgsWebEnginePage, with the specified parent widget.

Parameters:

parent (QObject|None = None)

documentSize(self) → QSize[source]

Returns the size of the page document, in pixels.

Warning

If the page content was NOT loaded using a blocking method, then this method involves running the event loop on the current thread. Take care when calling from the main thread as incorrect use will result in crashes.

Return type:

QSize

signal loadFinished(ok: bool)[source]

This signal is emitted when the page finishes loading content.

This signal is independent of script execution or page rendering.

ok will indicate whether the load was successful or any error occurred.

Parameters:

ok (bool)

signal loadProgress(progress: int)[source]

This signal is emitted when the global progress status changes.

The current value is provided by progress and scales from 0 to 100. It accumulates changes from all the child frames.

Parameters:

progress (int)

signal loadStarted[source]

This signal is emitted when the page starts loading content.

static printToPdfBlocking(html: str | None, pdfFileName: str | None, width: int, settings: QgsHtmlRenderSettings = QgsHtmlRenderSettings(), feedback: QgsFeedback | None = None)[source]

Renders HTML content to a PDF file asynchronously.

This method is safe to call on either the main or background threads, and will safely ensure that the main thread event loop is NOT run during the HTML rendering or PDF generation.

Parameters:
  • html (str|None) – HTML content

  • pdfFileName (str|None) – destination filename for PDF

  • width (int) – page width

  • settings (QgsHtmlRenderSettings = QgsHtmlRenderSettings()) – optional HTML render settings

  • feedback (QgsFeedback|None = None) – optional feedback for early cancellation

Returns:

  • True if the printing was successful

  • errorMessage: a descriptive error message if the printing fails

Added in version 4.4.

printToPdfBlocking(url: QUrl, pdfFileName: str|None, width: int, settings: QgsHtmlRenderSettings = QgsHtmlRenderSettings(), feedback: QgsFeedback|None = None) -> (bool, str) Renders HTML content to a PDF file asynchronously.

This method is safe to call on either the main or background threads, and will safely ensure that the main thread event loop is NOT run during the HTML rendering or PDF generation.

Parameters:
  • url – URL of HTML content

  • pdfFileName – destination filename for PDF

  • width – page width

  • settings – optional HTML render settings

  • feedback – optional feedback for early cancellation

Return type:

(bool, str)

Returns:

  • True if the printing was successful

  • errorMessage: a descriptive error message if the printing fails

Added in version 4.4.

render(self, painter: QPainter | None, painterRect: QRectF) → bool[source]

Renders the web page contents to a painter. Content will be rendered as vector objects.

The painterRect argument specifies the target rectangle for the page in painter coordinates.

Warning

This method involves running the event loop on the current thread. Take care when calling from the main thread as incorrect use will result in crashes.

Warning

This method requires a QGIS build with PDF4Qt library support.

Return type:

bool

Returns:

True if rendering was successful

Raises:

QgsNotSupportedException – on QGIS builds without PDF4Qt library support.

Parameters:
  • painter (QPainter|None)

  • painterRect (QRectF)

static renderBlocking(html: str | None, painter: QPainter | None, painterRect: QRectF, settings: QgsHtmlRenderSettings = QgsHtmlRenderSettings(), feedback: QgsFeedback | None = None)[source]

Renders HTML content to a painter asynchronously. Content will be rendered as vector objects.

This method is safe to call on either the main or background threads, and will safely ensure that the main thread event loop is NOT run during the HTML rendering.

Parameters:
  • html (str|None) – HTML content

  • painter (QPainter|None) – destination QPainter

  • painterRect (QRectF) – specifies the target rectangle for the page in painter coordinates

  • settings (QgsHtmlRenderSettings = QgsHtmlRenderSettings()) – optional HTML render settings

  • feedback (QgsFeedback|None = None) – optional feedback for early cancellation

Warning

This method requires a QGIS build with PDF4Qt library support.

Returns:

  • True if rendering was successful

  • errorMessage: a descriptive error message if the printing fails

  • renderedSize: the actual size of the rendered HTML

Raises:

QgsNotSupportedException – on QGIS builds without PDF4Qt library support.

Added in version 4.4.

renderBlocking(url: QUrl, painter: QPainter|None, painterRect: QRectF, settings: QgsHtmlRenderSettings = QgsHtmlRenderSettings(), feedback: QgsFeedback|None = None) -> (bool, str, QSizeF) Renders HTML content to a painter asynchronously. Content will be rendered as vector objects.

This method is safe to call on either the main or background threads, and will safely ensure that the main thread event loop is NOT run during the HTML rendering.

Parameters:
  • url – URL of HTML content

  • painter – destination QPainter

  • painterRect – specifies the target rectangle for the page in painter coordinates

  • settings – optional HTML render settings

  • feedback – optional feedback for early cancellation

Warning

This method requires a QGIS build with PDF4Qt library support.

Return type:

(bool, str, QSizeF)

Returns:

  • True if rendering was successful

  • errorMessage: a descriptive error message if the printing fails

  • renderedSize: the actual size of the rendered HTML

Raises:

QgsNotSupportedException – on QGIS builds without PDF4Qt library support.

Added in version 4.4.

setContent(self, data: QByteArray | bytes | bytearray | memoryview, mimeType: str | None = '', baseUrl: QUrl = QUrl(), blocking: bool = False) → bool[source]

Sets the content of the web page to data. If the mimeType argument is empty, it is assumed that the content is text/plain,charset=US-ASCII

The baseUrl is optional and used to resolve relative URLs in the document, such as referenced images or stylesheets.

If blocking is True then the call will block while the HTML is loaded. Otherwise the html is loaded immediately; external objects are loaded asynchronously.

Warning

Setting blocking to True involves running the event loop on the current thread. Take care when calling from the main thread as incorrect use will result in crashes.

Return type:

bool

Returns:

True if loading was successful

Parameters:
  • data (QByteArray|bytes|bytearray|memoryview)

  • mimeType (str|None = '')

  • baseUrl (QUrl = QUrl())

  • blocking (bool = False)

setHtml(self, html: str | None, baseUrl: QUrl = QUrl(), blocking: bool = False) → bool[source]

Sets the content of this page to html.

The baseUrl is optional and used to resolve relative URLs in the document, such as referenced images or stylesheets.

If blocking is True then the call will block while the HTML is loaded. Otherwise the html is loaded immediately; external objects are loaded asynchronously.

Note

This function works only for HTML, for other mime types (such as XHTML and SVG) setContent() should be used instead.

Warning

Setting blocking to True involves running the event loop on the current thread. Take care when calling from the main thread as incorrect use will result in crashes.

Return type:

bool

Returns:

True if loading was successful

Parameters:
  • html (str|None)

  • baseUrl (QUrl = QUrl())

  • blocking (bool = False)

setUrl(self, url: QUrl, blocking: bool = False) → bool[source]

Sets the url of the web page to be displayed.

Setting this property clears the page and loads the URL.

If blocking is True then the call will block while the HTML is loaded. Otherwise the html is loaded immediately; external objects are loaded asynchronously.

Warning

Setting blocking to True involves running the event loop on the current thread. Take care when calling from the main thread as incorrect use will result in crashes.

Return type:

bool

Returns:

True if loading was successful

Parameters:
  • url (QUrl)

  • blocking (bool = False)