Qt显示图片时必须是本地的或者qrc资源文件里的图片,而qml的Image确可以显示网络的图片,这是一个大的优势,不用下载图片到本地。下面请看源码示例:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("qml显示网络图片")
Image {
id: imageHeader
sourceSize.width: 300
sourceSize.height: 300
anchors.centerIn: parent
source: "qrc:/image/lasa.jpg"
}
Rectangle {
id: buttonRect
width: 100;
height: 50
color: "red"
anchors.top: parent.top
anchors.left: parent.left
MouseArea {
anchors.fill: parent
onClicked: {
//网络图片路径
imageHeader.source = "http://www.gov.cn/xhtml/2016gov/images/guoqing/bigmap.jpg"
}
}
}
}
运行结果 点击红色按钮修改url: