Qt 将动图(.gif)变为对象加入到主窗口中显示

目录

程序解析

代码示例(注释都在代码中)


程序解析

如何显示动图参考我的Qt 显示动图(.gif)文章

声明:这个功能虽然实现起来有些麻烦,但是它的好处是可以把许多的动图整理到一个界面上,并且每个动图都是一个单独的个体(对象),可以实现各自的功能

这里需要用到QGraphicsView类和QGraphicsScene类

头文件:

#include <QGraphicsView>
#include <QGraphicsScene>

然后在mainwindow.cpp中进行如下定义:

QGraphicsView *view=new QGraphicsView(this);//创建QGraphicsView对象并加入mainwindow界面
view->move(0,0);
view->resize(w,h);
view->show();
QRect rect(0,0,w,h);
QGraphicsScene *scene=new QGraphicsScene(rect);//创建QGraphicsScene对象
view->setStyleSheet("background: transparent;border:0px");
view->setScene(scene);//将scene加入view

此处的w、h是表示mainwindow的界面大小,根据需要替换即可

然后需要另外创建个类(如:Flower),用以显示动图和实现与动图相关的功能

这里需要用到QGraphicsItem类,头文件:

#include <QGraphicsItem>

类的继承方式:

class 类名: public QLabel,public QGraphicsItem

并在类中添加以下两行,以保证日后添加功能时能用:

    Q_OBJECT
    Q_INTERFACES(QGraphicsItem)

此类中有个虚函数,需要在子类中实现:

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = Q_NULLPTR)Q_DECL_OVERRIDE;

建立好类后加入到scene中:

scene->addWidget(对象名);
scene->addItem(对象名);

具体如何操作见代码示例↓ 

代码示例(注释都在代码中)

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.resize(100,100);//设置mainwindow窗口界面大小
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "flower.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QGraphicsView *view=new QGraphicsView(this);//创建QGraphicsView对象并加入mainwindow界面
    view->move(0,0);
    view->resize(100,100);
    view->show();
    QRect rect(0,0,100,100);
    QGraphicsScene *scene=new QGraphicsScene(rect);//创建QGraphicsScene对象
    view->setStyleSheet("background: transparent;border:0px");
    view->setScene(scene);//将scene加入view

    Flower *flower=new Flower(0,0,"C:/Users/dengbenzhong/Desktop/temp/flower.gif");//创建Flower对象,显示动图

    //将flower加入scene,相当于是加入mainwindow界面
    scene->addWidget(flower);
    scene->addItem(flower);
}

MainWindow::~MainWindow()
{
    delete ui;
}

flower.h

#ifndef FLOWER_H
#define FLOWER_H

#include <QLabel>
#include <QGraphicsItem>
#include <QString>
#include <QRectF>
#include <QMovie>
#include <QLabel>

class Flower : public QLabel,public QGraphicsItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsItem)
public:
    Flower(int x,int y,QString path);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = Q_NULLPTR)Q_DECL_OVERRIDE;//虚函数
    QRectF boundingRect() const Q_DECL_OVERRIDE;

    int x,y;
    QString path;
};

#endif // FLOWER_H

flower.cpp

#include "flower.h"

Flower::Flower(int x, int y, QString path)
{
    this->x=x;
    this->y=y;
    this->path=path;

    QMovie* movie=new QMovie(path);//创建动图对象
    this->setMovie(movie);//Label添加动图
    movie->start();//启动动图
    this->setScaledContents(true);
    this->setGeometry(x,y,100,100);
    setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
    QLabel::show();
}

void Flower::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

}

QRectF Flower::boundingRect() const
{
    QRectF qRectF(this->x,this->y,100,100);
    return qRectF;
}

运行结果:

如有疑问欢迎私信或评论区留言!

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值