QT开发之gif动画显示

此文从

https://blog.csdn.net/qq_21792169/article/details/53520626

(作者:Linux_Google)稍加注释而来,

main.cpp默认。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMovie>

namespace Ui {
class MainWindow;
}

class QMovie;
class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_horizontalSlider_valueChanged(int value);

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_spinBox_valueChanged(int value);

    void on_pushButton_4_clicked();

private:
    Ui::MainWindow *ui;
    QMovie *movie;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMovie>
#include <QString>
#include <QDebug>

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

    ui->label->setAlignment(Qt::AlignCenter);               //在两个维度对齐
    ui->label->setBackgroundRole(QPalette::Dark);          //label的黑影
    ui->label->setAutoFillBackground(true);                   //窗口小部件背景是否自动填充

    movie = new QMovie(this);                                           //实例movie
    movie->setFileName("./3.gif");                                     //设置gif路径

    movie->setCacheMode(QMovie::CacheAll);                  //缓存全部帧

    QSize size = ui->label->size();                                     //保存label的大小(调整后的)
    movie->setScaledSize(size);                                         //将缩放后的帧大小设置为大小
    ui->label->setMovie(movie);


    ui->horizontalSlider->setMinimum(0);                                      //滑块的最小值
    ui->horizontalSlider->setMaximum(movie->frameCount());      //滑块的最大值(影片的帧数)
    connect(movie, SIGNAL(frameChanged(int)),
            ui->horizontalSlider,SLOT(setValue(int)));                        //电影帧数改变值传递给滑块
    Imagelines = movie->frameCount();                                          //返回影片中的帧数
    qDebug() << "ImageLines" << Imagelines;                              //在调试窗口输出电影帧数

}

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

/* start buttons */
void MainWindow::on_pushButton_clicked()
{
    movie->start();                 //运行
}

/* stop buttons */
void MainWindow::on_pushButton_2_clicked()
{
    movie->stop();                  //停止播放电影
}

/* save pix */
void MainWindow::on_pushButton_3_clicked()
{
    int id = movie->currentFrameNumber();           //返回当前帧的序列号。 电影中第一帧的编号是0
    QPixmap pix = movie->currentPixmap();           //将当前帧作为Q像素图返回
    pix.save(QString("./%1.png").arg(id));              //将id的值替换%1,pix保存的文件名为./%1.png
    qDebug() << "save image";
}

void MainWindow::on_spinBox_valueChanged(int value)
{
    movie->setSpeed(value);                     //调整当前速度
}



void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    movie->jumpToFrame(value);                  //跳转到帧号
}

void MainWindow::on_pushButton_4_clicked()
{
    movie->setPaused(1);                            //暂停播放
    qDebug() << "stop is press!";
}

mainwindow.ui:

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ui version="4.0">  
  3.  <class>MainWindow</class>  
  4.  <widget class="QMainWindow" name="MainWindow">  
  5.   <property name="geometry">  
  6.    <rect>  
  7.     <x>0</x>  
  8.     <y>0</y>  
  9.     <width>400</width>  
  10.     <height>300</height>  
  11.    </rect>  
  12.   </property>  
  13.   <property name="windowTitle">  
  14.    <string>MainWindow</string>  
  15.   </property>  
  16.   <widget class="QWidget" name="centralWidget">  
  17.    <widget class="QPushButton" name="pushButton">  
  18.     <property name="geometry">  
  19.      <rect>  
  20.       <x>40</x>  
  21.       <y>190</y>  
  22.       <width>80</width>  
  23.       <height>25</height>  
  24.      </rect>  
  25.     </property>  
  26.     <property name="text">  
  27.      <string>开始</string>  
  28.     </property>  
  29.    </widget>  
  30.    <widget class="QPushButton" name="pushButton_2">  
  31.     <property name="geometry">  
  32.      <rect>  
  33.       <x>40</x>  
  34.       <y>240</y>  
  35.       <width>80</width>  
  36.       <height>25</height>  
  37.      </rect>  
  38.     </property>  
  39.     <property name="text">  
  40.      <string>停止</string>  
  41.     </property>  
  42.    </widget>  
  43.    <widget class="QPushButton" name="pushButton_3">  
  44.     <property name="geometry">  
  45.      <rect>  
  46.       <x>300</x>  
  47.       <y>190</y>  
  48.       <width>80</width>  
  49.       <height>25</height>  
  50.      </rect>  
  51.     </property>  
  52.     <property name="text">  
  53.      <string>截屏</string>  
  54.     </property>  
  55.    </widget>  
  56.    <widget class="QPushButton" name="pushButton_4">  
  57.     <property name="geometry">  
  58.      <rect>  
  59.       <x>170</x>  
  60.       <y>190</y>  
  61.       <width>80</width>  
  62.       <height>25</height>  
  63.      </rect>  
  64.     </property>  
  65.     <property name="text">  
  66.      <string>暂停</string>  
  67.     </property>  
  68.    </widget>  
  69.    <widget class="QSlider" name="horizontalSlider">  
  70.     <property name="geometry">  
  71.      <rect>  
  72.       <x>40</x>  
  73.       <y>150</y>  
  74.       <width>331</width>  
  75.       <height>16</height>  
  76.      </rect>  
  77.     </property>  
  78.     <property name="orientation">  
  79.      <enum>Qt::Horizontal</enum>  
  80.     </property>  
  81.     <property name="tickPosition">  
  82.      <enum>QSlider::TicksBelow</enum>  
  83.     </property>  
  84.     <property name="tickInterval">  
  85.      <number>10</number>  
  86.     </property>  
  87.    </widget>  
  88.    <widget class="QLabel" name="label">  
  89.     <property name="geometry">  
  90.      <rect>  
  91.       <x>20</x>  
  92.       <y>10</y>  
  93.       <width>361</width>  
  94.       <height>121</height>  
  95.      </rect>  
  96.     </property>  
  97.     <property name="text">  
  98.      <string>Linux_Google</string>  
  99.     </property>  
  100.    </widget>  
  101.    <widget class="QWidget" name="layoutWidget">  
  102.     <property name="geometry">  
  103.      <rect>  
  104.       <x>160</x>  
  105.       <y>240</y>  
  106.       <width>123</width>  
  107.       <height>26</height>  
  108.      </rect>  
  109.     </property>  
  110.     <layout class="QHBoxLayout" name="horizontalLayout">  
  111.      <item>  
  112.       <widget class="QLabel" name="label_2">  
  113.        <property name="text">  
  114.         <string>       进度</string>  
  115.        </property>  
  116.       </widget>  
  117.      </item>  
  118.      <item>  
  119.       <widget class="QSpinBox" name="spinBox">  
  120.        <property name="suffix">  
  121.         <string>%</string>  
  122.        </property>  
  123.        <property name="maximum">  
  124.         <number>999</number>  
  125.        </property>  
  126.        <property name="value">  
  127.         <number>100</number>  
  128.        </property>  
  129.       </widget>  
  130.      </item>  
  131.     </layout>  
  132.    </widget>  
  133.   </widget>  
  134.   <widget class="QMenuBar" name="menuBar">  
  135.    <property name="geometry">  
  136.     <rect>  
  137.      <x>0</x>  
  138.      <y>0</y>  
  139.      <width>400</width>  
  140.      <height>22</height>  
  141.     </rect>  
  142.    </property>  
  143.   </widget>  
  144.   <widget class="QToolBar" name="mainToolBar">  
  145.    <attribute name="toolBarArea">  
  146.     <enum>TopToolBarArea</enum>  
  147.    </attribute>  
  148.    <attribute name="toolBarBreak">  
  149.     <bool>false</bool>  
  150.    </attribute>  
  151.   </widget>  
  152.   <widget class="QStatusBar" name="statusBar"/>  
  153.  </widget>  
  154.  <layoutdefault spacing="6" margin="11"/>  
  155.  <resources/>  
  156.  <connections/>  
  157. </ui> 

注:mainwindow.ui不能直接在qt中改,以gedit打开,粘贴,然后重新打开工程即可

后两张为gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值