在Qt中可以用QMive加QLable显示Gif动,而在Qml中,有一个专门的控件显示Gif,即AnimatedImage
AnimatedImage 提供了五个属性:
currentFrame,指示当前正在播放的帧序号
frameCount,指示图片的总帧数
paused,表示是否暂停,设置它也可以暂停或继续播放
playing,指示动画是否在播放,默认为 true ,意思是 AnimatedImage 对象创建后立即开始播放
source,类型为 url ,指定要播放的图片地址,可以使本地文件、 qrc 里的资源、网络文件
使用代码示例如下
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
//color: "blue"
AnimatedImage {
id: animated
source: "qrc:/aa.gif"
onCurrentFrameChanged: {
info.text = ("%1/%2").arg(animated.currentFrame).arg(animated.frameCount)
}
}
Row {
spacing: 4
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
Text {
id: info
text: qsTr("text")
width: 80; height: 24;
color: "blue"
font.pixelSize: 20
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignRight
}
Button {
width: 60;
height: 24
text: (animated.paused == true) ? "Play" : "Pause";
onClicked: {
animated.paused = !animated.paused;
}
}
}
}
运行效果: