QWebView Class Reference

The QWebView class provides a widget that is used to view and edit web documents.

  1. #include <QWebView>

Inherits: QWidget.

This class was introduced in Qt 4.4.

Detailed Description

The QWebView class provides a widget that is used to view and edit web documents.

QWebView is the main widget component of the QtWebKit web browsing module. It can be used in various applications to display web content live from the Internet.

The image below shows QWebView previewed in Qt Designer with a Nokia website.

A web site can be loaded onto QWebView with the load() function. Like all Qt widgets, the show() function must be invoked in order to display QWebView. The snippet below illustrates this:

  1.     QWebView *view = new QWebView(parent);
  2.     view->load(QUrl("http://qt.nokia.com/"));
  3.     view->show();

Alternatively, setUrl() can also be used to load a web site. If you have the HTML content readily available, you can use setHtml() instead.

The loadStarted() signal is emitted when the view begins loading. The loadProgress() signal, on the other hand, is emitted whenever an element of the web view completes loading, such as an embedded image, a script, etc. Finally, the loadFinished() signal is emitted when the view has loaded completely. It's argument - either true or false - indicates load success or failure.

The page() function returns a pointer to the web page object. See Elements of QWebView for an explanation of how the web page is related to the view. To modify your web view's settings, you can access the QWebSettings object with the settings() function. With QWebSettings, you can change the default fonts, enable or disable features such as JavaScript and plugins.

The title of an HTML document can be accessed with the title() property. Additionally, a web site may also specify an icon, which can be accessed using the icon() property. If the title or the icon changes, the corresponding titleChanged() and iconChanged() signals will be emitted. The textSizeMultiplier() property can be used to change the overall size of the text displayed in the web view.

If you require a custom context menu, you can implement it by reimplementing contextMenuEvent() and populating your QMenu with the actions obtained from pageAction(). More functionality such as reloading the view, copying selected text to the clipboard, or pasting into the view, is also encapsulated within the QAction objects returned by pageAction(). These actions can be programmatically triggered using triggerPageAction(). Alternatively, the actions can be added to a toolbar or a menu directly. QWebView maintains the state of the returned actions but allows modification of action properties such as text or icon.

A QWebView can be printed onto a QPrinter using the print() function. This function is marked as a slot and can be conveniently connected to QPrintPreviewDialog's paintRequested() signal.

If you want to provide support for web sites that allow the user to open new windows, such as pop-up windows, you can subclass QWebView and reimplement the createWindow() function.

Elements of QWebView

QWebView consists of other objects such as QWebFrame and QWebPage. The flowchart below shows these elements are related.

Note: It is possible to use QWebPage and QWebFrame, without using QWebView, if you do not require QWidget attributes. Nevertheless, QtWebKit depends on QtGui, so you should use a QApplication instead of QCoreApplication.

See also Previewer Example, Web Browser, Form Extractor Example, Google Chat Example, and Fancy Browser Example.

Properties

Obsolete function
Toggle detailsiconicon : const QIcon

This property holds the icon associated with the web page currently viewed

By default, this property contains a null icon.

See also iconChanged() and QWebSettings::iconForUrl().

Access functions:

    Look up this member in the source code.

    Toggle detailsmodifiedmodified : const bool

    This property holds whether the document was modified by the user

    Parts of HTML documents can be editable for example through the contenteditable attribute on HTML elements.

    By default, this property is false.

    Access functions:

      Look up this member in the source code.

      Toggle detailsrenderHintsrenderHints : QPainter::RenderHints

      This property holds the default render hints for the view

      These hints are used to initialize QPainter before painting the Web page.

      QPainter::TextAntialiasing is enabled by default.

      Note: This property is not available on Symbian. However, the getter and setter functions can still be used directly.

      See also QPainter::renderHints().

      Access functions:

      Look up this member in the source code.

      Toggle detailsselectedTextselectedText : const QString

      This property holds the text currently selected

      By default, this property contains an empty string.

      See also findText() and selectionChanged().

      Access functions:

        Look up this member in the source code.

        Toggle detailstitletitle : const QString

        This property holds the title of the web page currently viewed

        By default, this property contains an empty string.

        See also titleChanged().

        Access functions:

          Look up this member in the source code.

          Toggle detailsurlurl : QUrl

          This property holds the url of the web page currently viewed

          Setting this property clears the view and loads the URL.

          By default, this property contains an empty, invalid URL.

          See also load() and urlChanged().

          Access functions:

          • void setUrl ( const QUrl & url )

          Look up this member in the source code.

          Toggle detailszoomFactorzoomFactor : qreal

          This property holds the zoom factor for the view

          Access functions:

          Look up this member in the source code.

          Toggle detailstextSizeMultipliertextSizeMultiplier : const qreal

          This property holds the scaling factor for all text in the frame

          Use setZoomFactor instead, in combination with the ZoomTextOnly attribute in QWebSettings.

          Note: Setting this property also enables the ZoomTextOnly attribute in QWebSettings.

          By default, this property contains a value of 1.0.

          Access functions:

            Look up this member in the source code.

            Public Functions

            Toggle details QWebView

            QWebViewQWebView ( QWidget *parent=0 ) ( QWidget *parent=0 )

            Constructs an empty QWebView with parent parent.

            See also load().

            Look up this member in the source code.

            Toggle details QWebView

            ~QWebView~QWebView () () [virtual]

            Destroys the web view.

            Look up this member in the source code.

            Toggle details void QWebView

            changeEventchangeEvent ( QEvent *e ) ( QEvent *e ) [virtual protected]

            Reimplemented from QWidget::changeEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            contextMenuEventcontextMenuEvent ( QContextMenuEvent *ev ) ( QContextMenuEvent *ev ) [virtual protected]

            Reimplemented from QWidget::contextMenuEvent().

            Look up this member in the source code.

            Toggle details QWebView * QWebView

            createWindowcreateWindow ( QWebPage::WebWindowType type ...) ( QWebPage::WebWindowType type ) [virtual protected]

            This function is called from the createWindow() method of the associated QWebPage, each time the page wants to create a new window of the given type. This might be the result, for example, of a JavaScript request to open a document in a new window.

            Note: If the createWindow() method of the associated page is reimplemented, this method is not called, unless explicitly done so in the reimplementation.

            See also QWebPage::createWindow().

            Look up this member in the source code.

            Toggle details void QWebView

            dragEnterEventdragEnterEvent ( QDragEnterEvent *ev ) ( QDragEnterEvent *ev ) [virtual protected]

            Reimplemented from QWidget::dragEnterEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            dragLeaveEventdragLeaveEvent ( QDragLeaveEvent *ev ) ( QDragLeaveEvent *ev ) [virtual protected]

            Reimplemented from QWidget::dragLeaveEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            dragMoveEventdragMoveEvent ( QDragMoveEvent *ev ) ( QDragMoveEvent *ev ) [virtual protected]

            Reimplemented from QWidget::dragMoveEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            dropEventdropEvent ( QDropEvent *ev ) ( QDropEvent *ev ) [virtual protected]

            Reimplemented from QWidget::dropEvent().

            Look up this member in the source code.

            Toggle details bool QWebView

            eventevent ( QEvent *e ) ( QEvent *e ) [virtual]

            Reimplemented from QObject::event().

            Look up this member in the source code.

            Toggle details bool QWebView

            findTextfindText ( const QString &subString , QWebPage::FindFlags options=0 ...) ( const QString &subString , QWebPage::FindFlags options=0 )

            Finds the specified string, subString, in the page, using the given options.

            If the HighlightAllOccurrences flag is passed, the function will highlight all occurrences that exist in the page. All subsequent calls will extend the highlight, rather than replace it, with occurrences of the new string.

            If the HighlightAllOccurrences flag is not passed, the function will select an occurrence and all subsequent calls will replace the current occurrence with the next one.

            To clear the selection, just pass an empty string.

            Returns true if subString was found; otherwise returns false.

            See also selectedText() and selectionChanged().

            Look up this member in the source code.

            Toggle details void QWebView

            focusInEventfocusInEvent ( QFocusEvent *ev ) ( QFocusEvent *ev ) [virtual protected]

            Reimplemented from QWidget::focusInEvent().

            Look up this member in the source code.

            Toggle details bool QWebView

            focusNextPrevChildfocusNextPrevChild ( bool next ) ( bool next ) [virtual protected]

            Reimplemented from QWidget::focusNextPrevChild().

            Look up this member in the source code.

            Toggle details void QWebView

            focusOutEventfocusOutEvent ( QFocusEvent *ev ) ( QFocusEvent *ev ) [virtual protected]

            Reimplemented from QWidget::focusOutEvent().

            Look up this member in the source code.

            Toggle details QWebHistory * QWebView

            historyhistory () ()const

            Returns a pointer to the view's history of navigated web pages.

            It is equivalent to

            1.     view->page()->history();

            Look up this member in the source code.

            Toggle details void QWebView

            inputMethodEventinputMethodEvent ( QInputMethodEvent *e ) ( QInputMethodEvent *e ) [virtual protected]

            Reimplemented from QWidget::inputMethodEvent().

            Look up this member in the source code.

            Toggle details QVariant QWebView

            inputMethodQueryinputMethodQuery ( Qt::InputMethodQuery property ...) ( Qt::InputMethodQuery property )const [virtual]

            Reimplemented from QWidget::inputMethodQuery().

            Look up this member in the source code.

            Toggle details void QWebView

            keyPressEventkeyPressEvent ( QKeyEvent *ev ) ( QKeyEvent *ev ) [virtual protected]

            Reimplemented from QWidget::keyPressEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            keyReleaseEventkeyReleaseEvent ( QKeyEvent *ev ) ( QKeyEvent *ev ) [virtual protected]

            Reimplemented from QWidget::keyReleaseEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            loadload ( const QUrl &url ) ( const QUrl &url )

            Loads the specified url and displays it.

            Note: The view remains the same until enough data has arrived to display the new url.

            See also setUrl(), url(), urlChanged(), and QUrl::fromUserInput().

            Look up this member in the source code.

            Toggle details void QWebView

            loadload ( const QNetworkRequest &request , QNetworkAccessManager::Operation operation=QNetworkAccessManager::GetOperation , const QByteArray &body=QByteArray() ...) ( const QNetworkRequest &request , QNetworkAccessManager::Operation operation=QNetworkAccessManager::GetOperation , const QByteArray &body=QByteArray() )

            Loads a network request, request, using the method specified in operation.

            body is optional and is only used for POST operations.

            Note: The view remains the same until enough data has arrived to display the new url.

            See also url() and urlChanged().

            Look up this member in the source code.

            Toggle details void QWebView

            mouseDoubleClickEventmouseDoubleClickEvent ( QMouseEvent *ev ) ( QMouseEvent *ev ) [virtual protected]

            Reimplemented from QWidget::mouseDoubleClickEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            mouseMoveEventmouseMoveEvent ( QMouseEvent *ev ) ( QMouseEvent *ev ) [virtual protected]

            Reimplemented from QWidget::mouseMoveEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            mousePressEventmousePressEvent ( QMouseEvent *ev ) ( QMouseEvent *ev ) [virtual protected]

            Reimplemented from QWidget::mousePressEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            mouseReleaseEventmouseReleaseEvent ( QMouseEvent *ev ) ( QMouseEvent *ev ) [virtual protected]

            Reimplemented from QWidget::mouseReleaseEvent().

            Look up this member in the source code.

            Toggle details QWebPage * QWebView

            pagepage () ()const

            Returns a pointer to the underlying web page.

            See also setPage().

            Look up this member in the source code.

            Toggle details QAction * QWebView

            pageActionpageAction ( QWebPage::WebAction action ) ( QWebPage::WebAction action )const

            Returns a pointer to a QAction that encapsulates the specified web action action.

            Look up this member in the source code.

            Toggle details void QWebView

            paintEventpaintEvent ( QPaintEvent *ev ) ( QPaintEvent *ev ) [virtual protected]

            Reimplemented from QWidget::paintEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            resizeEventresizeEvent ( QResizeEvent *e ) ( QResizeEvent *e ) [virtual protected]

            Reimplemented from QWidget::resizeEvent().

            Look up this member in the source code.

            Toggle details void QWebView

            setContentsetContent ( const QByteArray &data , const QString &mimeType=QString() , const QUrl &baseUrl=QUrl() ...) ( const QByteArray &data , const QString &mimeType=QString() , const QUrl &baseUrl=QUrl() )

            Sets the content of the web view to the specified content data. If the mimeType argument is empty it is currently assumed that the content is HTML but in future versions we may introduce auto-detection.

            External objects referenced in the content are located relative to baseUrl.

            The data is loaded immediately; external objects are loaded asynchronously.

            See also load(), setHtml(), and QWebFrame::toHtml().

            Look up this member in the source code.

            Toggle details void QWebView

            setHtmlsetHtml ( const QString &html , const QUrl &baseUrl=QUrl() ...) ( const QString &html , const QUrl &baseUrl=QUrl() )

            Sets the content of the web view to the specified html.

            External objects such as stylesheets or images referenced in the HTML document are located relative to baseUrl.

            The html is loaded immediately; external objects are loaded asynchronously.

            When using this method, WebKit assumes that external resources such as JavaScript programs or style sheets are encoded in UTF-8 unless otherwise specified. For example, the encoding of an external script can be specified through the charset attribute of the HTML script tag. Alternatively, the encoding can also be specified by the web server.

            See also load(), setContent(), and QWebFrame::toHtml().

            Look up this member in the source code.

            Toggle details void QWebView

            setPagesetPage ( QWebPage *page ) ( QWebPage *page )

            Makes page the new web page of the web view.

            The parent QObject of the provided page remains the owner of the object. If the current document is a child of the web view, it will be deleted.

            See also page().

            Look up this member in the source code.

            Toggle details void QWebView

            setRenderHintsetRenderHint ( QPainter::RenderHint hint , bool enabled=true ...) ( QPainter::RenderHint hint , bool enabled=true )

            If enabled is true, enables the specified render hint; otherwise disables it.

            See also renderHints and QPainter::renderHints().

            Look up this member in the source code.

            Toggle details void QWebView

            setTextSizeMultipliersetTextSizeMultiplier ( qreal factor ) ( qreal factor )

            Sets the value of the multiplier used to scale the text in a Web page to the factor specified.

            See also textSizeMultiplier().

            Look up this member in the source code.

            Toggle details QWebSettings * QWebView

            settingssettings () ()const

            Returns a pointer to the view/page specific settings object.

            It is equivalent to

            1.     view->page()->settings();

            See also QWebSettings::globalSettings().

            Look up this member in the source code.

            Toggle details QSize QWebView

            sizeHintsizeHint () ()const [virtual]

            Reimplemented from QWidget::sizeHint().

            Look up this member in the source code.

            Toggle details qreal QWebView

            textSizeMultipliertextSizeMultiplier () ()const

            Returns the value of the multiplier used to scale the text in a Web page.

            See also setTextSizeMultiplier().

            Look up this member in the source code.

            Toggle details void QWebView

            triggerPageActiontriggerPageAction ( QWebPage::WebAction action , bool checked=false ...) ( QWebPage::WebAction action , bool checked=false )

            Triggers the specified action. If it is a checkable action the specified checked state is assumed.

            The following example triggers the copy action and therefore copies any selected text to the clipboard.

            1.     view->triggerAction(QWebPage::Copy);

            See also pageAction().

            Look up this member in the source code.

            Toggle details void QWebView

            wheelEventwheelEvent ( QWheelEvent *ev ) ( QWheelEvent *ev ) [virtual protected]

            Reimplemented from QWidget::wheelEvent().

            Look up this member in the source code.

              Signals

              Toggle details void QWebView

              iconChangediconChanged () () [signal]

              This signal is emitted whenever the icon of the page is loaded or changes.

              In order for icons to be loaded, you will need to set an icon database path using QWebSettings::setIconDatabasePath().

              See also icon() and QWebSettings::setIconDatabasePath().

              Look up this member in the source code.

              Toggle details void QWebView

              linkClickedlinkClicked ( const QUrl &url ) ( const QUrl &url ) [signal]

              This signal is emitted whenever the user clicks on a link and the page's linkDelegationPolicy property is set to delegate the link handling for the specified url.

              See also QWebPage::linkDelegationPolicy().

              Look up this member in the source code.

              Toggle details void QWebView

              loadFinishedloadFinished ( bool ok ) ( bool ok ) [signal]

              This signal is emitted when a load of the page is finished. ok will indicate whether the load was successful or any error occurred.

              See also loadStarted().

              Look up this member in the source code.

              Toggle details void QWebView

              loadProgressloadProgress ( int progress ) ( int progress ) [signal]

              This signal is emitted every time an element in the web page completes loading and the overall loading progress advances.

              This signal tracks the progress of all child frames.

              The current value is provided by progress and scales from 0 to 100, which is the default range of QProgressBar.

              See also loadStarted() and loadFinished().

              Look up this member in the source code.

              Toggle details void QWebView

              loadStartedloadStarted () () [signal]

              This signal is emitted when a new load of the page is started.

              See also loadProgress() and loadFinished().

              Look up this member in the source code.

              Toggle details void QWebView

              selectionChangedselectionChanged () () [signal]

              This signal is emitted whenever the selection changes.

              See also selectedText().

              Look up this member in the source code.

              Toggle details void QWebView

              statusBarMessagestatusBarMessage ( const QString &text ) ( const QString &text ) [signal]

              This signal is emitted when the status bar text is changed by the page.

              Look up this member in the source code.

              Toggle details void QWebView

              titleChangedtitleChanged ( const QString &title ) ( const QString &title ) [signal]

              This signal is emitted whenever the title of the main frame changes.

              See also title().

              Look up this member in the source code.

              Toggle details void QWebView

              urlChangedurlChanged ( const QUrl &url ) ( const QUrl &url ) [signal]

              This signal is emitted when the url of the view changes.

              See also url() and load().

              Look up this member in the source code.

                Public Slots

                Toggle details void QWebView

                backback () () [slot]

                Convenience slot that loads the previous document in the list of documents built by navigating links. Does nothing if there is no previous document.

                It is equivalent to

                1.     view->page()->triggerPageAction(QWebPage::GoBack);

                See also forward() and pageAction().

                Look up this member in the source code.

                Toggle details void QWebView

                forwardforward () () [slot]

                Convenience slot that loads the next document in the list of documents built by navigating links. Does nothing if there is no next document.

                It is equivalent to

                1.     view->page()->triggerPageAction(QWebPage::GoForward);

                See also back() and pageAction().

                Look up this member in the source code.

                Toggle details void QWebView

                printprint ( QPrinter *printer ) ( QPrinter *printer )const [slot]

                Prints the main frame to the given printer.

                See also QWebFrame::print() and QPrintPreviewDialog.

                Look up this member in the source code.

                Toggle details void QWebView

                reloadreload () () [slot]

                Reloads the current document.

                See also stop(), pageAction(), and loadStarted().

                Look up this member in the source code.

                Toggle details void QWebView

                stopstop () () [slot]

                Convenience slot that stops loading the document.

                It is equivalent to

                1.     view->page()->triggerPageAction(QWebPage::Stop);

                See also reload(), pageAction(), and loadFinished().

                Look up this member in the source code.

                  Notes provided by the Qt Community
                  Informative
                  • 5

                  Votes: 4

                  Coverage: Qt library 4.7, 4.8

                  Picture of Volker Volker

                  Ant Farmer
                  35 notes

                  Nokia Certified Qt Developer

                  Printing does not work in 4.7.2 and 4.7.3

                  Printing a web page with print(QPrinter *printer) does not work with Qt 4.7.2 and 4.7.3 on Windows.

                  See QTBUG-19571 [bugreports.qt.nokia.com] and QTBUG-19525 [bugreports.qt.nokia.com] for a patch that fixes the issue, in case you must stick to one of the mentioned versions. QTBUG-17987 [bugreports.qt.nokia.com] is related.

                  [Revisions]