Qt Quick提供了一个类 AnimatedImage ,可以播放 Gif 动画,使用简单,这里是一个示例。
这里是用到的 Gif 图片:
AnimatedImage 提供了五个属性:
playGIF 示例可以播放、暂停 GIF ,显示当前帧和总帧数,还有一个退出按钮,很简单。
新建一个 Qt Quick App 项目,把 shapes.gif 添加到 qrc 文件中,我们在 QML 代码中通过 "qrc:/shapes.gif" 来访问它。
main.qml 内容如下:
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
Window {
visible: true;
width: animated.width;
height: animated.height + 40;
AnimatedImage {
id: animated;
source: "qrc:/shapes.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: 4;
Text {
id: info;
width: 60;
height: 24;
color: "red";
verticalAlignment: Text.AlignVCenter;
horizontalAlignment: Text.AlignRight;
}
Button {
width: 60;
height: 24;
text: (animated.paused == true) ? "Play" : "Pause";
onClicked: animated.paused = !animated.paused;
}
Button {
width: 60;
height: 24;
text: "Quit";
onClicked: Qt.quit();
}
}
}下面是程序运行时正在播放的效果,“播放/暂停”按钮显示 Pause :
下面是暂停时的效果:
OK ,示例就酱紫了。
回顾一下我的Qt Quick系列文章:
原文地址:http://blog.csdn.net/foruok/article/details/41677211