目录
1.案例描述
在很多软件的开起动画上会显示软件文件加载进度和加载文件名称,有的也会在开启动画上面显示各种按钮,用于用户选择想要开启的软件效果。本案例使用QWidget作为父窗口,背景为gif格式的动画循环播放显示,在QWidget中添加QPushbutton和QLabel控件,运行后控件显示在动画背景上。动画背景显示原理是使用QMovie播放gif动画,然后使用计时器每隔40ms触发QPainterEvent绘制一张QMovie当前帧图片。最终效果如下图。
案例软件,运行可查看效果,https://download.csdn.net/download/weixin_44322043/89574563
2.代码设计
Qt Creator工程文件如下
widgetanimationbackground.ui界面布局如下
widgetanimationbackground.pro代码内容如下
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
TARGET = HappyNewYear
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widgetanimationbackground.cpp
HEADERS += \
widgetanimationbackground.h
FORMS += \
widgetanimationbackground.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widgetanimationbackground.h代码内容如下
#pragma execution_character_set("utf-8")
#ifndef WIDGETANIMATIONBACKGROUND_H
#define WIDGETANIMATIONBACKGROUND_H
#include <QWidget>
#include <QFile>
#include <QMovie>
#include <QPainter>
#include <QPaintEvent>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class WidgetAnimationBackground; }
QT_END_NAMESPACE
class WidgetAnimationBackground : public QWidget
{
Q_OBJECT
public:
WidgetAnimationBackground(QWidget *parent = nullptr);
~WidgetAnimationBackground();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::WidgetAnimationBackground *ui;
void paintEvent(QPaintEvent *e);
QMovie *movie;
QTimer *timer;
};
#endif // WIDGETANIMATIONBACKGROUND_H
widgetanimationbackground.cpp代码内容如下
#include "widgetanimationbackground.h"
#include "ui_widgetanimationbackground.h"
WidgetAnimationBackground::WidgetAnimationBackground(QWidget *parent)
: QWidget(parent)
, ui(new Ui::WidgetAnimationBackground)
{
ui->setupUi(this);
QString startingLogoPath = QApplication::applicationDirPath() + "/HappyNewYear.gif";
movie = new QMovie(startingLogoPath);
movie->start();
this->resize(movie->frameRect().width(),movie->frameRect().height());
timer = new QTimer;
connect(timer,&QTimer::timeout,this,[=]{
update();//40ms刷新一次动画背景
});
timer->start(40);
}
void WidgetAnimationBackground::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.drawPixmap(0,0,this->width(),this->height(),movie->currentPixmap());//在widget中绘制动画帧图片
}
WidgetAnimationBackground::~WidgetAnimationBackground()
{
delete ui;
}
void WidgetAnimationBackground::on_pushButton_clicked()
{
ui->label->setText("Happy New Year");
}
void WidgetAnimationBackground::on_pushButton_2_clicked()
{
ui->label->setText("新年快乐");
}
main.cpp文件无改动。