Qt Reference Documentation

QML Image Element

[Inherits Item]

The Image element allows you to add bitmaps to a scene. More...

Inherited by AnimatedImage.

Properties

Detailed Description

An Image element displays a specified source image:

 import Qt 4.7

 Image { source: "qtlogo.png" }

If a size is not specified explicitly, the Image element is sized to the loaded image. Image elements can be stretched and tiled using the fillMode property.

If the image source is a network resource, the image is loaded asynchronous and the progress and status properties are updated appropriately. Otherwise, if the image is available locally, it is loaded immediately and the user interface is blocked until loading is complete. (This is typically the correct behavior for user interface elements.) For large local images, which do not need to be visible immediately, it may be preferable to enable asynchronous loading. This loads the image in the background using a low priority thread.

Images are cached and shared internally, so if several Image elements have the same source only one copy of the image will be loaded.

Note: Images are often the greatest user of memory in QML user interfaces. It is recommended that images which do not form part of the user interface have their size bounded via the sourceSize property. This is especially important for content that is loaded from external sources or provided by the user.

See also Image example.


Property Documentation

asynchronous : bool

Specifies that images on the local filesystem should be loaded asynchronously in a separate thread. The default value is false, causing the user interface thread to block while the image is loaded. Setting asynchronous to true is useful where maintaining a responsive user interface is more desireable than having images immediately visible.

Note that this property is only valid for images read from the local filesystem. Images loaded via a network resource (e.g. HTTP) are always loaded asynchonously.


fillMode : enumeration

Set this property to define what happens when the image set for the item is smaller than the size of the item.

  • Image.Stretch - the image is scaled to fit
  • Image.PreserveAspectFit - the image is scaled uniformly to fit without cropping
  • Image.PreserveAspectCrop - the image is scaled uniformly to fill, cropping if necessary
  • Image.Tile - the image is duplicated horizontally and vertically
  • Image.TileVertically - the image is stretched horizontally and tiled vertically
  • Image.TileHorizontally - the image is stretched vertically and tiled horizontally

Stretch (default)

 Image {
     width: 130; height: 100
     smooth: true
     source: "qtlogo.png"
 }

PreserveAspectFit

 Image {
     width: 130; height: 100
     fillMode: Image.PreserveAspectFit
     smooth: true
     source: "qtlogo.png"
 }

PreserveAspectCrop

 Image {
     width: 130; height: 100
     fillMode: Image.PreserveAspectCrop
     smooth: true
     source: "qtlogo.png"
 }

Tile

 Image {
     width: 120; height: 120
     fillMode: Image.Tile
     source: "qtlogo.png"
 }

TileVertically

 Image {
     width: 120; height: 120
     fillMode: Image.TileVertically
     source: "qtlogo.png"
 }

TileHorizontally

 Image {
     width: 120; height: 120
     fillMode: Image.TileHorizontally
     source: "qtlogo.png"
 }


pixmap : QPixmap

This property holds the QPixmap image to display.

This is useful for displaying images provided by a C++ implementation, for example, a model may provide a data role of type QPixmap.


progress : real

This property holds the progress of image loading, from 0.0 (nothing loaded) to 1.0 (finished).

See also status.


smooth : bool

Set this property if you want the image to be smoothly filtered when scaled or transformed. Smooth filtering gives better visual quality, but is slower. If the image is displayed at its natural size, this property has no visual or performance effect.

Note: Generally scaling artifacts are only visible if the image is stationary on the screen. A common pattern when animating an image is to disable smooth filtering at the beginning of the animation and reenable it at the conclusion.


source : url

Image can handle any image format supported by Qt, loaded from any URL scheme supported by Qt.

The URL may be absolute, or relative to the URL of the component.


sourceSize : QSize

This property holds the size of the loaded image, in pixels.

This is used to control the storage used by a loaded image. Unlike the width and height properties, which scale the painting of the image, this property affects the number of pixels stored.

If the image's actual size is larger than the sourceSize, the image is scaled down. If only one dimension of the size is set to greater than 0, the other dimension is set in proportion to preserve the source image's aspect ratio. (The fillMode is independent of this.)

If the source is an instrinsically scalable image (eg. SVG), this property determines the size of the loaded image regardless of intrinsic size. Avoid changing this property dynamically; rendering an SVG is slow compared to an image.

If the source is a non-scalable image (eg. JPEG), the loaded image will be no greater than this property specifies. For some formats (currently only JPEG), the whole image will never actually be loaded into memory.

Note: Changing this property dynamically will lead to the image source being reloaded, potentially even from the network if it is not in the disk cache.

Here is an example that ensures the size of the image in memory is no larger than 1024x1024 pixels, regardless of the size of the Image element.

 Image {
    anchors.fill: parent
    source: "images/reallyBigImage.jpg"
    sourceSize.width: 1024
    sourceSize.height: 1024
 }

The example below ensures the memory used by the image is no more than necessary to display the image at the size of the Image element. Of course if the Image element is resized a costly reload will be required, so use this technique only when the Image size is fixed.

 Image {
    anchors.fill: parent
    source: "images/reallyBigImage.jpg"
    sourceSize.width: width
    sourceSize.height: height
 }

status : enumeration

This property holds the status of image loading. It can be one of:

  • Image.Null - no image has been set
  • Image.Ready - the image has been loaded
  • Image.Loading - the image is currently being loaded
  • Image.Error - an error occurred while loading the image

Use this status to provide an update or respond to the status change in some way. For example, you could:

Trigger a state change:

 State { name: 'loaded'; when: image.status = Image.Ready }

Implement an onStatusChanged signal handler:

 Image {
     id: image
     onStatusChanged: if (image.status == Image.Ready) console.log('Loaded')
 }

Bind to the status value:

 Text { text: image.status != Image.Ready ? 'Not Loaded' : 'Loaded' }

See also progress.


X