標簽(空格分隔): Qt
Image可以顯示一個圖片,只要是Qt支持的,比如JPG、PNG、BMP、GIF、SVG等都可以顯示。它只能顯示靜態(tài)圖片,對于GIF等格式,只會把第一幀顯示出來。
顯示網(wǎng)路圖片
下面的例子是在下載和加載前顯示一個轉(zhuǎn)圈圈的Loading圖標,圖片加載成功后隱藏Loading圖標,如果加載出錯,則顯示一個簡單的錯誤消息
import QtQuick 2.0
import QtQuick.Controls 2.2
Rectangle {
width: 480;
height: 320;
color: "#d5d5d5";
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;
}
else if(imageViewer.status === Image.Ready){
busy.running = false;
}
else if(imageViewer.status === Image.Error){
busy.running = false;
stateLabel.visible = true;
stateLabel.text = "ERROR";
}
}
}
Component.onCompleted: {
imageViewer.source = "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=1354031076,1682582909&fm=173&app=49&f=JPEG?w=218&h=146&s=8FA3FE01024025475E915E020300E0D5"
}
}
BusyIndicator 用來顯示一個等待圖元,在進行一些耗時操作時你可以使用它來緩解用戶的焦躁情緒。
BusyIndicator 的running屬性是個布爾值,為true時顯示。style屬性是個布爾值,為true時顯示。style屬性允許你定制BusyIndicator。默認的效果就是如下圖所示,一個轉(zhuǎn)圈圈的動畫。
BusyIndicator {
id:busy;
running: true;
anchors.centerIn: parent;
z:2;

BusyIndicator .gif
雖然BusyIndicator只有running和style兩個屬性,但它的祖先有很多屬性,上面用到的anchors和z,都是從Item繼承來的屬性,可以直接使用。