QWidget动画背景,QT控件显示在动画之上

目录

1.案例描述

 2.代码设计


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文件无改动。

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

99.999...%

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值