QML Image Element

The Image element displays an image in a declarative user interface More...

Image元素在一个声明式的用户接口中显示一张图片。

Inherits Item

Inherited by AnimatedImage.

Properties

Detailed Description

The Image element is used to display images in a declarative user interface.

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 the AnimatedImageelement.

图片通过source属性来指定来源。Image支持各种标准格式的图片,包括bitmap,png,jpeg,svg。如果想显示图片动画,请使用AnimatedImage元素。

If the width and height properties are not specified, the Image element automatically uses the size of the loaded image. By default, specifying the width and height of the element 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.

如果width和height属性没有指定,则使用图片真实的width和height。通常是通过指定width和height来实现图片缩放,这里可以通过设置fillMode属性来设置是否允许图片拉伸或是平铺。

Example Usage

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

下面一个例子显示了一个Image元素的最简单用法。

  1. import QtQuick 1.0
  2. Image {
  3. source: "pics/qtlogo.png"
  4. }
  1. <span style="font-size:18px;"> import QtQuick 1.0
  2. Image {
  3. source: "pics/qtlogo.png"
  4. }
  5. </span>



Performance

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 property.

默认情况下,程序启动前会马上加载本地图片,而用户接口则等到图片加载完了之后才开始显示。如果加载的图片比较大则等待时间会比较长,这时可以将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.

如果图片是来自网络下载,则asynchronous属性自动为真,而progress属性和status属性也会随着下载的进度自动更新。

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.

注意:图片往往是QML中消耗内存最多,建议将用户界面不需要的图片的size自动绑定到其sourceSize,特别是作为外部资源图片或是用户提供的图片。

See also Image example and QDeclarativeImageProvider.

Property Documentation

asynchronous : bool

Specifies that images on the local file system 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.

默认值为假,此时用户接口线程会阻塞知道将图片加载完。如果asynchronous为真则会在另外一个线程中加载图片,不会阻塞用户接口线程。

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.

fillMode属性用来定义当图片大小设置小于item大小时的填充方式。

  • Image.Stretch - the image is scaled to fit
  • 此时图片会被拉伸到item的大小。Image.Stretch是fillMode的默认填充方式。
  • 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)

  1. Image {
  2. width: 130; height: 100
  3. smooth: true
  4. source: "qtlogo.png"
  5. }
  1. <span style="font-size:18px;"> Image {
  2. width: 130; height: 100
  3. smooth: true
  4. source: "qtlogo.png"
  5. }
  6. </span>


PreserveAspectFit

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

PreserveAspectCrop

  1. Image {
  2. width: 130; height: 100
  3. fillMode: Image.PreserveAspectCrop
  4. smooth: true
  5. source: "qtlogo.png"
  6. clip: true
  7. }
  1. <span style="font-size:18px;"> Image {
  2. width: 130; height: 100
  3. fillMode: Image.PreserveAspectCrop
  4. smooth: true
  5. source: "qtlogo.png"
  6. clip: true
  7. }
  8. </span>


Tile

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

TileVertically

  1. Image {
  2. width: 120; height: 120
  3. fillMode: Image.TileVertically
  4. smooth: true
  5. source: "qtlogo.png"
  6. }
  1. <span style="font-size:18px;"> Image {
  2. width: 120; height: 120
  3. fillMode: Image.TileVertically
  4. smooth: true
  5. source: "qtlogo.png"
  6. }
  7. </span>


TileHorizontally

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

See also Image example.


read-onlypaintedWidth : real

read-onlypaintedHeight : 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 a fillMode PreserveAspectFit or fillMode PreserveAspectCrop paintedWidth or paintedHeight can be smaller or larger than width and height of the Image element.


read-onlyprogress : real

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

progress属性表明图片加载进度,0.0表示没有加载,1.0表示加载完成。

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.

如果设置smooth属性为真则当图片缩放或是向量变换会给予图片比较好的显示效果,但这将导致速度变慢。如果图片显示是其固有大小则没有影响。

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.

See also QDeclarativeImageProvider.


sourceSize : QSize

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

sourceSize属性指明了加载图片的实际宽度和高度。

Unlike the 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 values:

width/height属性用来缩放图像,而QSize实际上指出了内存中加载这个图片在水平方向和垂直方向上的像素数,即这个图片的分辨率。在下面这个例子中,限制了图片的分辨率为1024X1024,而不管图片的width属性和height属性。

  1. Rectangle {
  2. width: ...
  3. height: ...
  4. Image {
  5. anchors.fill: parent
  6. source: "reallyBigImage.jpg"
  7. sourceSize.width: 1024
  8. sourceSize.height: 1024
  9. }
  10. }
  1. <span style="font-size:18px;"> Rectangle {
  2. width: ...
  3. height: ...
  4. Image {
  5. anchors.fill: parent
  6. source: "reallyBigImage.jpg"
  7. sourceSize.width: 1024
  8. sourceSize.height: 1024
  9. }
  10. }
  11. </span>
 
 
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.
如果图片本质上就支持缩放(例如SVG),这个属性将决定加载图片的大小而不管这个图片本质上的大小。对于SVG图片应该避免动态改变这个属性,因为渲染SVG图片跟普通图片相比速度要慢。

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.

对于不可缩放的图片(例如JPEG),加载的图片将不会比这个属性大。实际上对于JPEG图片来说,整个图片并没有真正全部加载到内存中。

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

注意这个属性不能动态改变,否则图片奖重新加载,如果这个图片来自网络则重新下载。


read-onlystatus : enumeration

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

status属性表明图片加载的状态。可取如下值:

  • Image.Null - no image has been set
  • Image.Null 表明没有设置图片
  • Image.Ready - the image has been loaded
  • Image.Ready 表明图片已经加载完毕
  • Image.Loading - the image is currently being loaded
  • Image.Loading 表明图片正在加载
  • Image.Error - an error occurred while loading the image
  • Image.Error 表明加载图片时出错

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

我们可以使用status来做一些针对图片加载完毕后的一些处理。如下例所示:

  • Trigger a state change:

     State { name: 'loaded'; when: image.status == Image.Ready }
  • Implement an onStatusChanged signal handler:
    1. Image {
    2. id: image
    3. onStatusChanged: if (image.status == Image.Ready) console.log('Loaded')
    4. }
    1. <span style="font-size:18px;"> Image {
    2. id: image
    3. onStatusChanged: if (image.status == Image.Ready) console.log('Loaded')
    4. }
    5. </span>
    
    
  • Bind to the status value:
     Text { text: image.status == Image.Ready ? 'Loaded' : 'Not loaded' }
 
0

QML Image Element的更多相关文章

  1. QML Flipable、Flickable和状态与动画 上篇

    本文介绍的是QML Flipable.Flickable和状态与动画,我们以前接触过QML组件,和一些QML相关的内容,那么本文介绍的内容就很明了了.先来看内容. AD:51CTO 网+ 第十二期沙龙 ...

  2. QML学习【一】Basic Types

      QML入门教程(1) QML是什么? QML是一种描述性的脚本语言,文件格式以.qml结尾.语法格式非常像CSS(参考后文具体例子),但又支持javacript形式的编程控制.它结合了QtDesi ...

  3. qml操作播放器

    现在增加了一个filter属性,所以可以很好和opencv结合.转一篇文章(http://blog.qt.io/blog/2015/03/20/introducing-video-filters-in ...

  4. qt 学习之路 :QML 语法

    前面我们已经见识过 QML 文档.一个 QML 文档分为 import 和对象声明两部分.如果你要使用 Qt Quick,就需要 import QtQuick 2.QML 是一种声明语言,用于描述程序 ...

  5. QML 语言基础

    在<Qt Quick 简单介绍>中我们提到 QML 语法和 Json 相似,请參考<Qt on Android: http下载与Json解析>查看 Json 语法.当然这里我们 ...

  6. Qt(QML)本地化

    Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all  Literial UI strings ...

  7. OpenLayers学习笔记(三)— QML与HTML通信之 地图上点击添加自由文本

    实现在地图随意点击,弹出文本输入框,输入任意文字,完成自由文本添加的功能 作者: 狐狸家的鱼 GitHub:八至 本文链接:地图上点击添加自由文本 关于如何QML与HTML通信已经在上一篇文章 QML ...

  8. QML 从入门到放弃

    发现了一个问题: QQuickView only supports loading of root objects that derive from QQuickItem. If your examp ...

  9. QT qml TreeView展示数据结构于界面

    Class  QAbstractItemModel: 使用QML的TreeView类来展示树状的结构,对应的是QT的Model/View模型.这个model是一个数据模型,要为TreeView提供一个 ...

随机推荐

  1. string整理

    /* scanf是c语言的,而string是c++的类,所以不能使用scanf直接读入 */ #include<cstdio> #include<string>//注意支持库 ...

  2. 应急分析异常通信的小思路和自己写的小工具(查询CNAME和A记录)

    一.背景: 在很多时候,应急会发现.卧槽,异常连接,只有一个域名或者IP. 怎么办?上防火墙看记录,查域名对应的记录累成狗,自己把之前的代码改了改,写了个小工具,一条命令查询DNS相关记录,也可以指定 ...

  3. 用httpclient做压力测试时Too many open files的解决办法

    在工作过程中,用httpclient去压测一个web api,发现压一小段时间就出现了Too many open files.实际上,HttpClient建立Socket时 ,post.release ...

  4. IPTABLES简介

    iptables防火墙工作原理 简介:iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防火墙基于内核编码实现,具有非常稳定的性能和高效率: iptables属 ...

  5. webpack4学习笔记(一)

    webpack4 1,安装webpack npm insatll webpack --save-dev //安装最新版本 npm insatll webpack@<version> --s ...

  6. Python面象对象与类

    # -*- coding: utf-8 -*- # @Date: 2017-08-26 # @Original: from collections import namedtuple from col ...

  7. 剑指Offer——左旋转字符串

    题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”ab ...

  8. 一.shell基础知识

    参考网站:http://billie66.github.io/TLCL/book/chap08.html 1.字符“*”--展开 [me@linuxbox ~]$ echo * Desktop Doc ...

  9. python web框架 django 工程 创建 目录介绍

    # 创建Django工程django-admin startproject [工程名称] 默认创建django 项目都会自带这些东西 django setting 配置文件 django可以配置缓存 ...

  10. 002-java语言基础

    一.安装卸载 卸载:控制面板 安装:下载对应版本 注意1.安装路径→尽量不要有空格和汉字 注意2.安装之后,jre可以不用安装,jdk中含有 二.环境变量 环境变量:理解,一些快捷路径.方便快速查找应 ...