QWebEngineDownloadItem Class

The QWebEngineDownloadItem class provides information about a download. More...

Header: #include <QWebEngineDownloadItem>
qmake: QT += webenginewidgets
Since: Qt 5.5
Inherits: QObject

Public Types

enum DownloadInterruptReason { NoReason, FileFailed, FileAccessDenied, FileNoSpace, ..., UserCanceled }
enum DownloadState { DownloadRequested, DownloadInProgress, DownloadCompleted, DownloadCancelled, DownloadInterrupted }
enum SavePageFormat { UnknownSaveFormat, SingleHtmlSaveFormat, CompleteHtmlSaveFormat, MimeHtmlSaveFormat }

Public Functions

quint32 id() const
QWebEngineDownloadItem::DownloadInterruptReason interruptReason() const
QString interruptReasonString() const
bool isFinished() const
bool isPaused() const
bool isSavePageDownload() const
QString mimeType() const
QWebEnginePage *page() const
QString path() const
qint64 receivedBytes() const
QWebEngineDownloadItem::SavePageFormat savePageFormat() const
void setPath(QString path)
void setSavePageFormat(QWebEngineDownloadItem::SavePageFormat format)
QWebEngineDownloadItem::DownloadState state() const
qint64 totalBytes() const
QUrl url() const
  • 31 public functions inherited from QObject

Public Slots

void accept()
void cancel()
void pause()
void resume()
  • 1 public slot inherited from QObject

Signals

void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void finished()
void isPausedChanged(bool isPaused)
void stateChanged(QWebEngineDownloadItem::DownloadState state)

Static Public Members

const QMetaObject staticMetaObject
  • 9 static public members inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 9 protected functions inherited from QObject

Detailed Description

The QWebEngineDownloadItem class provides information about a download.

QWebEngineDownloadItem models a download throughout its life cycle, starting with a pending download request and finishing with a completed download. It can be used, for example, to get information about new downloads, to monitor progress, and to pause, resume, and cancel downloads.

Downloads are usually triggered by user interaction on a web page. It is the QWebEngineProfile's responsibility to notify the application of new download requests, which it does by emitting the downloadRequested signal together with a newly created QWebEngineDownloadItem. The application can then examine this item and decide whether to accept it or not. A signal handler must explicitly call accept() on the item for Qt WebEngine to actually start downloading and writing data to disk. If no signal handler calls accept(), then the download request will be automatically rejected and nothing will be written to disk.

Note: Some properties, like the path under which the file will be saved, can only be changed before calling accept().

Object Life Cycle

All items are guaranteed to be valid during the emission of the downloadRequested signal. If accept() is not called by any signal handler, then the item will be deleted immediately after signal emission. This means that the application must not keep references to rejected download items. It also means the application should not use a queued connection to this signal.

If accept() is called by a signal handler, then the QWebEngineProfile will take ownership of the item. However, it is safe for the application to delete the item at any time, except during the handling of the downloadRequested signal. The QWebEngineProfile being a long-lived object, it is in fact recommended that the application delete any items it is no longer interested in.

Note: Deleting an item will also automatically cancel a download since 5.12.2, but it is recommended to cancel manually before deleting for portability.

Web Page Downloads

In addition to normal file downloads, which consist simply of retrieving some raw bytes from the network and writing them to disk, Qt WebEngine also supports saving complete web pages, which involves parsing the page's HTML, downloading any dependent resources, and potentially packaging everything into a special file format (savePageFormat). To check if a download is for a file or a web page, use isSavePageDownload.

See also QWebEngineProfile, QWebEngineProfile::downloadRequested, QWebEnginePage::download, and QWebEnginePage::save.

Member Type Documentation

enum QWebEngineDownloadItem::DownloadInterruptReason

Describes the reason why a download was interrupted:

ConstantValueDescription
QWebEngineDownloadItem::NoReason0Unknown reason or not interrupted.
QWebEngineDownloadItem::FileFailed1General file operation failure.
QWebEngineDownloadItem::FileAccessDenied2The file cannot be written locally, due to access restrictions.
QWebEngineDownloadItem::FileNoSpace3Insufficient space on the target drive.
QWebEngineDownloadItem::FileNameTooLong5The directory or file name is too long.
QWebEngineDownloadItem::FileTooLarge6The file size exceeds the file system limitation.
QWebEngineDownloadItem::FileVirusInfected7The file is infected with a virus.
QWebEngineDownloadItem::FileTransientError10Temporary problem (for example the file is in use, out of memory, or too many files are opened at once).
QWebEngineDownloadItem::FileBlocked11The file was blocked due to local policy.
QWebEngineDownloadItem::FileSecurityCheckFailed12An attempt to check the safety of the download failed due to unexpected reasons.
QWebEngineDownloadItem::FileTooShort13An attempt was made to seek past the end of a file when opening a file (as part of resuming a previously interrupted download).
QWebEngineDownloadItem::FileHashMismatch14The partial file did not match the expected hash.
QWebEngineDownloadItem::NetworkFailed20General network failure.
QWebEngineDownloadItem::NetworkTimeout21The network operation has timed out.
QWebEngineDownloadItem::NetworkDisconnected22The network connection has been terminated.
QWebEngineDownloadItem::NetworkServerDown23The server has gone down.
QWebEngineDownloadItem::NetworkInvalidRequest24The network request was invalid (for example, the original or redirected URL is invalid, has an unsupported scheme, or is disallowed by policy).
QWebEngineDownloadItem::ServerFailed30General server failure.
QWebEngineDownloadItem::ServerBadContent33The server does not have the requested data.
QWebEngineDownloadItem::ServerUnauthorized34The server did not authorize access to the resource.
QWebEngineDownloadItem::ServerCertProblem35A problem with the server certificate occurred.
QWebEngineDownloadItem::ServerForbidden36Access forbidden by the server.
QWebEngineDownloadItem::ServerUnreachable37Unexpected server response (might indicate that the responding server may not be the intended server).
QWebEngineDownloadItem::UserCanceled40The user canceled the download.

This enum was introduced or modified in Qt 5.9.

enum QWebEngineDownloadItem::DownloadState

This enum describes the state of the download:

ConstantValueDescription
QWebEngineDownloadItem::DownloadRequested0Download has been requested, but has not been accepted yet.
QWebEngineDownloadItem::DownloadInProgress1Download is in progress.
QWebEngineDownloadItem::DownloadCompleted2Download completed successfully.
QWebEngineDownloadItem::DownloadCancelled3Download has been cancelled.
QWebEngineDownloadItem::DownloadInterrupted4Download has been interrupted (by the server or because of lost connectivity).

enum QWebEngineDownloadItem::SavePageFormat

This enum describes the format that is used to save a web page.

ConstantValueDescription
QWebEngineDownloadItem::UnknownSaveFormat-1This is not a request for downloading a complete web page.
QWebEngineDownloadItem::SingleHtmlSaveFormat0The page is saved as a single HTML page. Resources such as images are not saved.
QWebEngineDownloadItem::CompleteHtmlSaveFormat1The page is saved as a complete HTML page, for example a directory containing the single HTML page and the resources.
QWebEngineDownloadItem::MimeHtmlSaveFormat2The page is saved as a complete web page in the MIME HTML format.

This enum was introduced or modified in Qt 5.7.

Member Function Documentation

[slot] void QWebEngineDownloadItem::accept()

Accepts the current download request, which will start the download.

If the item is in the DownloadRequested state, then it will transition into the DownloadInProgress state and the downloading will begin. If the item is in any other state, then nothing will happen.

See also finished() and stateChanged().

[slot] void QWebEngineDownloadItem::cancel()

Cancels the current download.

If the item is in the DownloadInProgress state, then it will transition into the DownloadCancelled state, the downloading will stop, and partially downloaded files will be deleted from disk.

If the item is in the DownloadCompleted state, then nothing will happen. If the item is in any other state, then it will transition into the DownloadCancelled state without further effect.

See also finished() and stateChanged().

[signal] void QWebEngineDownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)

This signal is emitted to indicate the progress of the download request.

The bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the size of the file to be downloaded is not known, bytesTotal will be 0.

See also totalBytes() and receivedBytes().

[signal] void QWebEngineDownloadItem::finished()

This signal is emitted when the download finishes.

See also state() and isFinished().

quint32 QWebEngineDownloadItem::id() const

Returns the download item's ID.

QWebEngineDownloadItem::DownloadInterruptReason QWebEngineDownloadItem::interruptReason() const

Returns the reason why the download was interrupted.

This function was introduced in Qt 5.9.

See also interruptReasonString().

QString QWebEngineDownloadItem::interruptReasonString() const

Returns a human-readable description of the reason for interrupting the download.

This function was introduced in Qt 5.9.

See also interruptReason().

bool QWebEngineDownloadItem::isFinished() const

Returns whether this download is finished (completed, cancelled, or non-resumable interrupted state).

See also finished() and state().

bool QWebEngineDownloadItem::isPaused() const

Returns whether this download is paused.

See also pause() and resume().

[signal] void QWebEngineDownloadItem::isPausedChanged(bool isPaused)

This signal is emitted whenever isPaused changes.

This function was introduced in Qt 5.10.

See also pause() and isPaused().

bool QWebEngineDownloadItem::isSavePageDownload() const

Returns true if this is a download request for saving a web page.

This function was introduced in Qt 5.11.

See also savePageFormat() and setSavePageFormat().

QString QWebEngineDownloadItem::mimeType() const

Returns the MIME type of the download.

This function was introduced in Qt 5.6.

QWebEnginePage *QWebEngineDownloadItem::page() const

Returns the page the download was requested on. If the download was not triggered by content in a page, nullptr is returned.

This function was introduced in Qt 5.12.

QString QWebEngineDownloadItem::path() const

Returns the full target path where data is being downloaded to.

The path includes the file name. The default suggested path is the standard download location and file name is deduced not to overwrite already existing files.

See also setPath().

[slot] void QWebEngineDownloadItem::pause()

Pauses the download.

Has no effect if the state is not DownloadInProgress. Does not change the state.

This function was introduced in Qt 5.10.

See also resume() and isPaused().

qint64 QWebEngineDownloadItem::receivedBytes() const

Returns the amount of data in bytes that has been downloaded so far.

-1 means the size is unknown.

[slot] void QWebEngineDownloadItem::resume()

Resumes the current download if it was paused or interrupted.

Has no effect if the state is not DownloadInProgress or DownloadInterrupted. Does not change the state.

This function was introduced in Qt 5.10.

See also pause(), isPaused(), and state().

QWebEngineDownloadItem::SavePageFormat QWebEngineDownloadItem::savePageFormat() const

Returns the format the web page will be saved in if this is a download request for a web page.

This function was introduced in Qt 5.7.

See also setSavePageFormat() and isSavePageDownload().

void QWebEngineDownloadItem::setPath(QString path)

Sets the full target path to download the file to.

The path should also include the file name. The download path can only be set in response to the QWebEngineProfile::downloadRequested() signal before the download is accepted. Past that point, this function has no effect on the download item's state.

See also path().

void QWebEngineDownloadItem::setSavePageFormat(QWebEngineDownloadItem::SavePageFormat format)

Sets the format the web page will be saved in if this is a download request for a web page.

This function was introduced in Qt 5.7.

See also savePageFormat() and isSavePageDownload().

QWebEngineDownloadItem::DownloadState QWebEngineDownloadItem::state() const

Returns the download item's current state.

See also DownloadState.

[signal] void QWebEngineDownloadItem::stateChanged(QWebEngineDownloadItem::DownloadState state)

This signal is emitted whenever the download's state changes.

See also state() and DownloadState.

qint64 QWebEngineDownloadItem::totalBytes() const

Returns the the total amount of data to download in bytes.

-1 means the size is unknown.

QUrl QWebEngineDownloadItem::url() const

Returns the download's origin URL.

© 2021 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.