WebView Element Reference

The WebView item allows you to add Web content to a canvas.

[Inherits Item]

This element was introduced in Qt 4.7.

Detailed Description

The WebView item allows you to add Web content to a canvas.

A WebView renders Web content based on a URL.

This type is made available by importing the QtWebKit module:

import QtWebKit 1.0

The WebView item includes no scrolling, scaling, toolbars, or other common browser components. These must be implemented around WebView. See the QML Web Browser example for a demonstration of this.

The page to be displayed by the item is specified using the url property, and this can be changed to fetch and display a new page. While the page loads, the progress property is updated to indicate how much of the page has been loaded.

Appearance

If the width and height of the item is not set, they will dynamically adjust to a size appropriate for the content. This width may be large for typical online web pages, typically greater than 800 by 600 pixels.

If the width or height is explictly set, the rendered Web site will be clipped, not scaled, to fit into the set dimensions.

If the preferredWidth property is set, the width will be this amount or larger, usually laying out the Web content to fit the preferredWidth.

The appearance of the content can be controlled to a certain extent by changing the settings.standardFontFamily property and other settings related to fonts.

The page can be zoomed by calling the heuristicZoom() method, which performs a series of tests to determine whether zoomed content will be displayed in an appropriate way in the space allocated to the item.

User Interaction and Navigation

By default, certain mouse and touch events are delivered to other items in preference to the Web content. For example, when a scrolling view is created by placing a WebView in a Flickable, move events are delivered to the Flickable so that the user can scroll the page. This prevents the user from accidentally selecting text in a Web page instead of scrolling.

The pressGrabTime property defines the time the user must touch or press a mouse button over the WebView before the Web content will receive the move events it needs to select text and images.

When this item has keyboard focus, all keyboard input will be sent directly to the Web page within.

When the navigates by clicking on links, the item records the pages visited in its internal history

Because this item is designed to be used as a component in a browser, it exposes actions for back, forward, reload and stop. These can be triggered to change the current page displayed by the item.

Example Usage

The following example displays a scaled down Web page at a fixed size.

  1. import QtWebKit 1.0
  2.  
  3. WebView  {
  4.     url: "http://www.nokia.com"
  5.     preferredWidth: 490
  6.     preferredHeight: 400
  7.     scale: 0.5
  8.     smooth: false
  9. }

Property Documentation

This property holds the action for causing the previous URL in the history to be displayed.

This property holds the action for causing the next URL in the history to be displayed.

This property holds HTML text set directly

The html property can be set as a string.

  1.                 WebView  {
  2.     html: "<p>This is <b>HTML</b>."
  3. }
  • icon : pixmap

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

  • javaScriptWindowObjects : list<object>

A list of QML objects to expose to the web page.

Each object will be added as a property of the web frame's window object. The property name is controlled by the value of WebView.windowObjectName attached property.

Exposing QML objects to a web page allows JavaScript executing in the web page itself to communicate with QML, by reading and writing properties and by calling methods of the exposed QML objects.

This example shows how to call into a QML method using a window object.

  1.                 WebView  {
  2.     javaScriptWindowObjects: QtObject  {
  3.         WebView.windowObjectName: "qml"
  4.  
  5.         function qmlCall()  {
  6.             console.log("This call is in QML!");
  7.         }
  8.     }
  9.  
  10.     html: "<script>console.log(\"This is in WebKit!\"); window.qml.qmlCall();</script>"
  11. }

The output of the example will be:

  1. This is in WebKit!
  2. This call is in QML!

If Javascript is not enabled for the page, then this property does nothing.

  • newWindowComponent : component

This property holds the component to use for new windows. The component must have a WebView somewhere in its structure.

When the web engine requests a new window, it will be an instance of this component.

The parent of the new window is set by newWindowParent. It must be set.

  • newWindowParent : item

The parent item for new windows.

See also newWindowComponent.

  • preferredHeight : int

This property holds the ideal height for displaying the current URL. This only affects the area zoomed by heuristicZoom().

  • preferredWidth : int

This property holds the ideal width for displaying the current URL.

  • pressGrabTime : int

The number of milliseconds the user must press before the WebView starts passing move events through to the Web engine (rather than letting other QML elements such as a Flickable take them).

Defaults to 400ms. Set to 0 to always grab and pass move events to the Web engine.

This property holds the progress of loading the current URL, from 0 to 1.

If you just want to know when progress gets to 1, use WebView::onLoadFinished() or WebView::onLoadFailed() instead.

This property holds the action for reloading with the current URL

  • settings.standardFontFamily : string
  • settings.fixedFontFamily : string
  • settings.serifFontFamily : string
  • settings.sansSerifFontFamily : string
  • settings.cursiveFontFamily : string
  • settings.fantasyFontFamily : string
  • settings.minimumFontSize : int
  • settings.minimumLogicalFontSize : int
  • settings.defaultFontSize : int
  • settings.defaultFixedFontSize : int
  • settings.autoLoadImages : bool
  • settings.javascriptEnabled : bool
  • settings.javaEnabled : bool
  • settings.pluginsEnabled : bool
  • settings.privateBrowsingEnabled : bool
  • settings.javascriptCanOpenWindows : bool
  • settings.javascriptCanAccessClipboard : bool
  • settings.developerExtrasEnabled : bool
  • settings.linksIncludedInFocusChain : bool
  • settings.zoomTextOnly : bool
  • settings.printElementBackgrounds : bool
  • settings.offlineStorageDatabaseEnabled : bool
  • settings.offlineWebApplicationCacheEnabled : bool
  • settings.localStorageDatabaseEnabled : bool
  • settings.localContentCanAccessRemoteUrls : bool

These properties give access to the settings controlling the web view.

See QWebSettings for details of these properties.

  1.                 WebView  {
  2.     settings.pluginsEnabled: true
  3.     settings.standardFontFamily: "Arial"
  4.     // ...
  5. }

This property is the current status suggested by the current web page. In a web browser, such status is often shown in some kind of status bar.

This property holds the action for stopping loading with the current URL

This property holds the title of the web page currently viewed

By default, this property contains an empty string.

This property holds the URL to the page displayed in this item. It can be set, but also can change spontaneously (eg. because of network redirection).

If the url is empty, the page is blank.

The url is always absolute (QML will resolve relative URL strings in the context of the containing QML document).

Signal Handler Documentation

  • WebView::onAlert ( string message )

The handler is called when the web engine sends a JavaScript alert. The message is the text to be displayed in the alert to the user.

  • WebView::onDoubleClick ( int clickx, int clicky )

The WebView does not pass double-click events to the web engine, but rather emits this signals.

  • WebView::onLoadFailed ()

This handler is called when the web engine fails loading a page or any component content (WebView::onLoadFinished() will be emitted on success).

  • WebView::onLoadFinished ()

This handler is called when the web engine successfully finishes loading a page, including any component content (WebView::onLoadFailed() will be emitted otherwise).

See also progress.

  • WebView::onLoadStarted ()

This handler is called when the web engine begins loading a page. Later, WebView::onLoadFinished() or WebView::onLoadFailed() will be emitted.

Method Documentation

  • bool WebView::evaluateJavaScript ( string scriptSource )

Evaluates the scriptSource JavaScript inside the context of the main web frame, and returns the result of the last executed statement.

Note that this JavaScript does not have any access to QML objects except as made available as windowObjects.

  • bool WebView::heuristicZoom ( int clickX, int clickY, real maxzoom )

Finds a zoom that:

  • shows a whole item
  • includes (clickX, clickY)
  • fits into the preferredWidth and preferredHeight
  • zooms by no more than maxZoom
  • is more than 10% above the current zoom

If such a zoom exists, emits zoomTo(zoom,centerX,centerY) and returns true; otherwise, no signal is emitted and returns false.

Notes provided by the Qt Community
Best Practice
  • 0

Votes: 0

Coverage: Qt library 4.7, 4.8

Picture of Alicemirror Alicemirror

Lab Rat
2 notes

Note on pressGrabTime usage

In this document you can read:
bq. The pressGrabTime property defines the time the user must touch or press a mouse button over the WebView before the Web content will receive the move events it needs to select text and images.

Mostly this property should be set enabling a short delay before the web page react to the user interaction instead of the touck event is delivered to other components of the page.
In the case where the user interaction is really oriented to the web page (i.e. a web script with a game that requires object-dragging) you should set

  1. pressGrabTime = 0

to avoid the delay that in this case is unwanted. For more information about and example of this case please read the following forum post: http://developer.qt.nokia.com/forums/viewthread/9874/

[Revisions]

Informative
  • 0

Votes: 0

Coverage: Qt library 4.7, 4.8

Picture of fcrochik fcrochik

Lab Rat
3 notes

Nokia Certified Qt Developer

Accessing qml from html

The javaScriptWindowObjects is quite powerful and allows the a javascript method on the HTML page (running in the browser) to communicate with the QML objects. evaluateJavaScript goes the other way

The example above is also a good example of attached properties in QML.

[Revisions]