QML之Image

Image控件可以用来显示图片,当然必须是qt支持的图片格式,比如Jpg,Png等等。当然,这里说的图片是静态图片,如果你要显示gif这种格式的图片,那么只能将gif的第一帧显示出来。

    Image {
        id: name
        width: 120 //图像宽度
        height: 120 //图像高度
        anchors.centerIn: parent
        source: "images/Contacts_white_round.png"
        autoTransform: true //图像是否自动变换,默认为false
        fillMode: Image.Pad //图像的填充方式-保持图片原样
        cache: false //指定是否缓存图像,默认为true,在处理大图像的时候,指定为false还是很有用的
        //horizontalAlignment: Image.Right //水平对齐方式 居右
         //horizontalAlignment: Image.Left //水平对齐方式 居左
        // horizontalAlignment: Image.AlignHCenter //水平对齐方式 居中 默认居中
    }

这里简单设置了图片的几个属性,让我们看下它的显示效果:
这里写图片描述

接下来看下图片的填充效果,具体含义我在代码中有注释:

 Column
    {

        Image {
            width: 48
            height: 48
             source: "images/Contacts_white_round.png"
            fillMode: Image.Pad //图像的填充方式-保持图片原样
            anchors.left: pad
        }

        Image {
            width: 32
            height: 32
             source: "images/Contacts_white_round.png"
            fillMode: Image.PreserveAspectFit //图像的填充方式-缩放不裁剪
        }

        Image {
            width: 32
            height: 32
            source: "images/Contacts_white_round.png"
            fillMode: Image.PreserveAspectCrop //图像的填充方式-缩放必要时裁剪
        }


        Image {
            width: 128
            height: 128
            source: "images/Contacts_white_round.png"
            fillMode: Image.Tile //图像的填充方式-水平垂直复制
        }

        Image {
            width: 128
            height: 128
            source: "images/Contacts_white_round.png"
            fillMode: Image. TileVertically //图像的填充方式-水平填充,垂直复制
        }

        Image {
            width: 128
            height: 128
            source: "images/Contacts_white_round.png"
            fillMode: Image.TileHorizontally //图像的填充方式-水平复制,垂直填充
        }
    }

运行结果:
这里写图片描述

Image中有一个比较有意思的属性,那就是mirror属性,这个属性指定的是图像是否水平旋转,在呈现镜像的场合是十分方便的。

 Row
    {
        Image {
                   width: 48
                   height: 48
                   source: "images/login-icon.jpg"
                  mirror: false
               }

        Image {
                   width: 48
                   height: 48
                   source: "images/login-icon.jpg"
                  mirror: true
               }

    }

看下效果:
这里写图片描述

感觉还不错。。大伙可以试一试。

Image控件还有一个值得注意的地方就是,它可以加载网络图片,它的source属性是一个URL,可以接收Qt支持的任意一种网络协议。当Image识别到source是网络资源的时候会自动开启异步加载。

让我们看一个加载网络图片的代码:

 Rectangle
    {
        width: 400
        height: 320
        color:"#ffffff"

        BusyIndicator
        {
            id:busy
            running: true
            anchors.centerIn: parent
            z:2
        }

        Text {
            id: stateLabel
            visible: false
            anchors.centerIn: parent
            z:3
        }

        Image {
            id:imageViews
            cache: false
            asynchronous: true
            fillMode: Image.PreserveAspectFit

            onStatusChanged: {
                if(imageViews.status == Image.Loading){
                    busy.running = true;
                    stateLabel.visible = false;
                    console.info("Lodings....");
                }
                else if(imageViews.status == Image.Ready)
                {
                    busy.running = false;
                    console.info("Ready....");
                }
                else if(imageViews.status == Image.Error)
                {
                    busy.running = false;
                    stateLabel.visible = true;
                    stateLabel.text = "Errors";
                    console.info("Errors....");

                }
            }
        }

        Component.onCompleted:
        {
            imageViews.source = "http://b79.photo.store.qq.com/psu?/7c595bd9-7aa7-4b74-92af-b0874c1528c8/ASXihnWbyI62kCamKmOGjQACj1HTNuIEgpyYcF5R5rw!/b/YdIhIS**bgAAYpG2JS9OawAA&bo=ngL2AQAAAAABFFg!&rf=viewer_4&t=5"
        }

    }

程序运行后会出现加载界面:
这里写图片描述

图片加载完成后,BusyIndicator消失,图片显示
这里写图片描述

这里主要使用onStatusChanged信号处理器来监听Status的状态变化来进行界面跟新。

### QML 中 `Image` 元素的使用方法 #### 基本概念 在 QML 中,`Image` 是一种用于显示图像资源的核心元素。它支持多种类型的图像源,包括本地文件路径、网络 URL 和自定义图像提供程序(如 `QQuickImageProvider`)。通过设置其属性,可以实现丰富的功能,例如缩放、裁剪和异步加载。 --- #### 属性说明 以下是 `Image` 的主要属性及其作用: 1. **source (string)** 定义要加载的图像资源路径。它可以是一个本地文件路径、URL 或由图像提供程序生成的内容。如果未指定,则不会显示任何图像[^1]。 2. **fillMode (enumeration)** 控制如何填充目标区域。常见的选项有: - `Image.Stretch`: 默认模式,拉伸以适应大小。 - `Image.PreserveAspectFit`: 保持宽高比并调整到适合容器的尺寸。 - `Image.PreserveAspectCrop`: 裁剪多余部分以适配容器。 3. **asynchronous (bool)** 启用异步加载机制。当启用此属性时,图像将在后台线程中加载,从而避免阻塞主线程。 4. **cache (bool)** 指定是否缓存该图像。默认情况下启用了缓存,但如果希望减少内存占用,可将其禁用。 5. **status (enumeration, readonly)** 表示当前图像的状态,可能值为: - `Image.Null`: 尚未尝试加载。 - `Image.Ready`: 图像已成功加载。 - `Image.Loading`: 正在加载过程中。 - `Image.Error`: 发生错误无法加载。 6. **smooth (bool)** 是否应用平滑变换来提高缩放质量,默认为 `true`。 7. **horizontalAlignment / verticalAlignment (enumeration)** 设置水平或垂直方向上的对齐方式,适用于图像小于容器的情况。 8. **mirrored (bool)** 如果设为 `true`,则会镜像翻转图像。 9. **progressiveRenderingEnabled (bool)** 对于渐进式 JPEG 文件,允许逐步渲染效果。 --- #### 示例代码 以下是一些典型的 `Image` 使用场景: ##### 示例 1: 显示本地图片 ```qml import QtQuick 2.15 import QtQuick.Window 2.15 Window { visible: true width: 640 height: 480 Image { id: localImage source: "qrc:/images/example.png" fillMode: Image.PreserveAspectFit anchors.centerIn: parent } } ``` ##### 示例 2: 异步加载远程图片 ```qml import QtQuick 2.15 import QtQuick.Window 2.15 Window { visible: true width: 640 height: 480 Image { id: remoteImage source: "https://example.com/image.jpg" asynchronous: true cache: false fillMode: Image.PreserveAspectFit anchors.fill: parent } Text { text: remoteImage.status === Image.Loading ? "Loading..." : "" anchors.horizontalCenter: parent.horizontalCenter color: "gray" } } ``` ##### 示例 3: 自定义图像提供器 假设已经注册了一个名为 `colors` 的 `QQuickAsyncImageProvider` 提供器[^2]: ```qml import QtQuick 2.15 import QtQuick.Window 2.15 Window { visible: true width: 640 height: 480 Image { id: customImage source: "image://colors/yellow" fillMode: Image.PreserveAspectFit anchors.centerIn: parent } } ``` --- #### 解决常见问题 1. **Invalid image provider 错误** 当遇到类似于 `QML Image: Invalid image provider: image://colors/yellow` 的错误时,通常是因为尚未正确注册对应的图像提供者类。确保已在 C++ 端调用 `engine.addImageProvider()` 方法完成初始化。 2. **性能优化建议** - 频繁使用的静态图片应开启缓存 (`cache: true`)。 - 大型动态图片推荐采用异步加载策略 (`asynchronous: true`)。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值