【qml】Image 显示网络图片

一、说明

Image类型可以用来显示一个图像。
使用source属性将图像的源指定为一个URL。可以用Qt支持的任何标准图像格式提供图像,包括位图格式(如PNG和JPEG)和矢量图形格式(如SVG)。如果你需要显示动画图像,使用AnimatedSprite或AnimatedImage。
如果没有指定宽度和高度属性,图像将自动使用已加载图像的大小。默认情况下,指定项目的宽度和高度将使图像缩放到该大小。这种行为可以通过set来改变

案例

在这里插入图片描述

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Item {
    width: 640
    height: 480
    visible: true
    anchors.fill: parent

    BusyIndicator{
        id : busy;
        running: true;
        anchors.centerIn: parent;
        z : 2;
    }
    Text {
        id: stateLabel;
        visible: false;
        anchors.centerIn: parent;
        z:3;
    }

    Image {
        id: imageViewer;
        asynchronous: true;
        cache: false;
        anchors.fill: parent;
        fillMode: Image.PreserveAspectFit;
        onStatusChanged: {
            if(imageViewer.status ===Image.Loading){
                busy.running = true;
                stateLabel.visible = false;
            }
            else if(imageViewer.status ===Image.Ready){
                busy.running = false;
            }
            else if(imageViewer.status === Image.Error){
                busy.running = false;
                stateLabel.visible = true;
                stateLabel.text = "Error"
            }
        }
    }
    property var urls: [
        "https://goss3.cfp.cn/creative/vcg/veer/800/new/VCG41N487648969.jpg",
        "https://goss4.cfp.cn/creative/vcg/800/new/VCG41N888067282.jpg",
        "http://pics0.baidu.com/feed/a686c9177f3e6709665d2ed0c933213bf9dc55bf.jpeg?token=c923a2fa85ba4a5283ebede1db712492"
    ]
    Component.onCompleted: {
        timer.start();
    }

    Timer{
        id: timer
        interval: 3000;
        repeat: true
        triggeredOnStart: true;
        property int idx: 0
        onTriggered: {
            console.log(idx%3)
            imageViewer.source = urls[idx%3];
            idx++;
        }
    }
}

qml 有一些控件控件默认支持network,比如网络图片,视频,音频,甚至是qml文件,有些功能还是很方便的,如果出现tsl问题,请拷贝openssl的dll文件到运行目录。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值