Image QML Type

Displays an image. 更多...

import 语句: import QtQuick
继承:

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
Image {
    source: "pics/qtlogo.png"
}
					

Compressed Texture Files

When supported by the implementation of the underlying graphics API at run time, images can also be supplied in compressed texture files. The content must be a simple RGB(A) format 2D texture. Supported compression schemes are only limited by the underlying driver and GPU. The following container file formats are supported:

注意: The intended vertical orientation of an image in a texture file is not generally well defined. Different texture compression tools have different defaults and options of when to perform vertical flipping of the input image. If an image from a texture file appears upside down, flipping may need to be toggled in the asset conditioning process. Alternatively, the Image element itself can be flipped by either applying a suitable transformation via the transform property or, more conveniently, by setting the mirrorVertically 特性:

transform: [ Translate { y: -myImage.height }, Scale { yScale: -1 } ]
				

or

mirrorVertically: true
				

注意: Semi-transparent original images require alpha pre-multiplication prior to texture compression in order to be correctly displayed in Qt Quick. This can be done with the following ImageMagick command line:

convert foo.png \( +clone -alpha Extract \) -channel RGB -compose Multiply -composite foo_pm.png
				

Do not confuse container formats, such as, KTX , and the format of the actual texture data stored in the container file. For example, reading a KTX file is supported on all platforms, independently of what GPU driver is used at run time. However, this does not guarantee that the compressed texture format, used by the data in the file, is supported at run time. For example, if the KTX file contains compressed data with the format ETC2 RGBA8 , and the 3D graphics API implementation used at run time does not support ETC2 compressed textures, the Image item will not display anything.

注意: Compressed texture format support is not under Qt's control, and it is up to the application or device developer to ensure the compressed texture data is provided in the appropriate format for the target environment(s).

Do not assume that compressed format support is specific to a platform. It may also be specific to the driver and 3D API implementation in use on that particular platform. In practice, implementations of different 3D graphics APIs (e.g., Vulkan and OpenGL) on the same platform (e.g., Windows) from the same vendor for the same hardware may offer a different set of compressed texture formats.

When targeting desktop environments (Windows, macOS, Linux) only, a general recommendation is to consider using the DXTn / BCn formats since these tend to have the widest support amongst the implementations of Direct 3D, Vulkan, OpenGL, and Metal on these platforms. In contrast, when targeting mobile or embedded devices, the ETC2 or ASTC formats are likely to be a better choice since these are typically the formats supported by the OpenGL ES implementations on such hardware.

An application that intends to run across desktop, mobile, and embedded hardware should plan and design its use of compressed textures carefully. It is highly likely that relying on a single format is not going to be sufficient, and therefore the application will likely need to branch based on the platform to use compressed textures in a format appropriate there, or perhaps to skip using compressed textures in some cases.

Automatic Detection of File Extension

source URL indicates a non-existing local file or resource, the Image element attempts to auto-detect the file extension. If an existing file can be found by appending any of the supported image file extensions to the source URL, then that file will be loaded.

The file search attempts to look for compressed texture container file extensions first. If the search is unsuccessful, it attempts to search with the file extensions for the conventional image file types 。例如:

// Assuming the "pics" directory contains the following files:
//   dog.jpg
//   cat.png
//   cat.pkm
Image {
    source: "pics/cat.png"     // loads cat.png
}
Image {
    source: "pics/dog"         // loads dog.jpg
}
Image {
    source: "pics/cat"         // normally loads cat.pkm, but if no OpenGL, loads cat.png instead.
}
				

This functionality facilitates deploying different image asset file types on different target platforms. This can be useful in order to tune application performance and adapt to different graphics hardware.

This functionality was introduced in Qt 5.11.

性能

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 范例 - 图像元素 , QQuickImageProvider ,和 QImageReader::setAutoDetectImageFormat ().

特性文档编制

paintedHeight : real [read-only]

paintedWidth : real [read-only]

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.

horizontalAlignment : enumeration

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 .

currentFrame : int

frameCount : int [read-only]

currentFrame is the frame that is currently visible. The default is 0 . You can set it to a number between 0 and frameCount - 1 to display a different frame, if the image contains multiple frames.

frameCount is the number of frames in the image. Most images have only one frame.

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。

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 范例 - 图像元素 .

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。

另请参阅 smooth .

mirror : bool

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

默认值为 false。

mirrorVertically : bool [since 6.2]

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

默认值为 false。

该特性在 Qt 6.2 引入。

progress : real [read-only]

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 , Compressed Texture Files ,和 Automatic Detection of File Extension .

sourceClipRect : rect

This property, if set, holds the rectangular region of the source image to be loaded.

The sourceClipRect works together with the sourceSize property to conserve system resources when only a portion of an image needs to be loaded.

Rectangle {
    width: ...
    height: ...
    Image {
       anchors.fill: parent
       source: "reallyBigImage.svg"
       sourceSize.width: 1024
       sourceSize.height: 1024
       sourceClipRect: Qt.rect(100, 100, 512, 512)
    }
}
				

In the above example, we conceptually scale the SVG graphic to 1024x1024 first, and then cut out a region of interest that is 512x512 pixels from a location 100 pixels from the top and left edges. Thus sourceSize determines the scale, but the actual output image is 512x512 pixels.

Some image formats are able to conserve CPU time by rendering only the specified region. Others will need to load the entire image first and then clip it to the specified region.

This property can be cleared to reload the entire image by setting sourceClipRect 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.

注意: Sub-pixel clipping is not supported: the given rectangle will be passed to QImageReader::setScaledClipRect ().

sourceSize : size

This property holds the scaled width and height of the full-frame image.

不像 width and height properties, which scale the painting of the image, this property sets the maximum 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.

sourceClipRect property is also set, sourceSize determines the scale, but it will be clipped to the size of the clip rectangle.

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.

另请参阅 Qt Quick Examples - Pointer Handlers .

status : enumeration [read-only]

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:

另请参阅 progress .

内容