Image QML Type

显示图像 更多...

import 语句: import QtQuick 2.7
继承: Item
继承者: AnimatedImage

特性

详细描述

The Image type displays an image.

The source of the image is specified as a URL using the source property. Images can be supplied in any of the standard image formats supported by Qt, including bitmap formats such as PNG and JPEG, and vector graphics formats such as SVG. If you need to display animated images, use AnimatedSprite or AnimatedImage .

width and height properties are not specified, the Image automatically uses the size of the loaded image. By default, specifying the width and height of the item causes the image to be scaled to that size. This behavior can be changed by setting the fillMode property, allowing the image to be stretched and tiled instead.

用法范例

The following example shows the simplest usage of the Image type.

import QtQuick 2.0
Image {
    source: "pics/qtlogo.png"
}
					

性能

By default, locally available images are loaded immediately, and the user interface is blocked until loading is complete. If a large image is to be loaded, it may be preferable to load the image in a low priority thread, by enabling the asynchronous 特性。

If the image is obtained from a network rather than a local resource, it is automatically loaded asynchronously, and the progress and status properties are updated as appropriate.

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

注意 : 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.

另请参阅 Qt Quick 范例 - 图像元素 and QQuickImageProvider .

特性文档编制

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 desirable 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 asynchronously.

autoTransform : bool

This property holds whether the image should automatically apply image transformation metadata such as EXIF orientation.

默认情况下,此特性被设为 false。

This QML property was introduced in Qt 5.5.

cache : bool

Specifies whether the image should be cached. The default value is true. Setting cache to false is useful when dealing with large images, to make sure that they aren't cached at the expense of small 'ui element' images.

fillMode : enumeration

Set this property to define what happens when the source image has a different size than 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
  • Image.Pad - the image is not transformed
Stretch (default)
Image {
    width: 130; height: 100
    source: "qtlogo.png"
}
												
PreserveAspectFit
Image {
    width: 130; height: 100
    fillMode: Image.PreserveAspectFit
    source: "qtlogo.png"
}
												
PreserveAspectCrop
Image {
    width: 130; height: 100
    fillMode: Image.PreserveAspectCrop
    source: "qtlogo.png"
    clip: true
}
												
Tile
Image {
    width: 120; height: 120
    fillMode: Image.Tile
    horizontalAlignment: Image.AlignLeft
    verticalAlignment: Image.AlignTop
    source: "qtlogo.png"
}
												
TileVertically
Image {
    width: 120; height: 120
    fillMode: Image.TileVertically
    verticalAlignment: Image.AlignTop
    source: "qtlogo.png"
}
												
TileHorizontally
Image {
    width: 120; height: 120
    fillMode: Image.TileHorizontally
    verticalAlignment: Image.AlignLeft
    source: "qtlogo.png"
}
												

注意, clip is false by default which means that the item might paint outside its bounding rectangle even if the fillMode is set to PreserveAspectCrop .

另请参阅 Qt Quick 范例 - 图像元素 .

horizontalAlignment : enumeration

Sets the horizontal and vertical alignment of the image. By default, the image is center aligned.

The valid values for horizontalAlignment are Image.AlignLeft , Image.AlignRight and Image.AlignHCenter . The valid values for verticalAlignment are Image.AlignTop , Image.AlignBottom and Image.AlignVCenter .

mipmap : bool

This property holds whether the image uses mipmap filtering when scaled or transformed.

Mipmap filtering gives better visual quality when scaling down compared to smooth, but it may come at a performance cost (both when initializing the image and during rendering).

默认情况下,此特性被设为 false。

This QML property was introduced in Qt 5.3.

另请参阅 smooth .

mirror : bool

This property holds whether the image should be horizontally inverted (effectively displaying a mirrored image).

默认值为 false。

paintedHeight : real

These properties hold the size of the image that is actually painted. In most cases it is the same as width and height , but when using an Image.PreserveAspectFit Image.PreserveAspectCrop paintedWidth or paintedHeight can be smaller or larger than width and height of the Image item.

paintedWidth : real

These properties hold the size of the image that is actually painted. In most cases it is the same as width and height , but when using an Image.PreserveAspectFit Image.PreserveAspectCrop paintedWidth or paintedHeight can be smaller or larger than width and height of the Image item.

progress : real

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

另请参阅 status .

smooth : bool

This property holds whether the image is smoothly filtered when scaled or transformed. Smooth filtering gives better visual quality, but it may be slower on some hardware. If the image is displayed at its natural size, this property has no visual or performance effect.

默认情况下,此特性被设为 true。

另请参阅 mipmap .

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.

另请参阅 QQuickImageProvider .

sourceSize : QSize

This property holds the actual width and height of the loaded image.

不像 width and height properties, which scale the painting of the image, this property sets the actual number of pixels stored for the loaded image so that large images do not use more memory than necessary. For example, this ensures the image in memory is no larger than 1024x1024 pixels, regardless of the Image's width and height 值:

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

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 both the sourceSize.width and sourceSize.height are set the image will be scaled down to fit within the specified size (unless PreserveAspectCrop or PreserveAspectFit are used, then it will be scaled to match the optimal size for cropping/fitting), maintaining the image's aspect ratio. The actual size of the image after scaling is available via Item::implicitWidth and Item::implicitHeight .

If the source is an intrinsically 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.

sourceSize can be cleared to the natural size of the image by setting sourceSize to undefined .

注意: Changing this property dynamically causes the image source to be reloaded, potentially even from the network, if it is not in the disk cache.

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
    
    
    ==
    
    
    图像
    
    .
    
    Ready
    
    )
    
    console
    
    .
    
    log
    
    (
    
    'Loaded'
    
    )
    }
    																					
  • Bind to the status value:
    Text { text: image.status == Image.Ready ? 'Loaded' : 'Not loaded' }
    																					

另请参阅 progress .

verticalAlignment : enumeration

Sets the horizontal and vertical alignment of the image. By default, the image is center aligned.

The valid values for horizontalAlignment are Image.AlignLeft , Image.AlignRight and Image.AlignHCenter . The valid values for verticalAlignment are Image.AlignTop , Image.AlignBottom and Image.AlignVCenter .