QT开发之gif动画显示

上篇文章我们调到了QSound音频,这篇文章我们讲解QMovie类来显示gif文件.

QMovie有三种运行状态,QMovie::NotRunning(),QMovie::Paused(),QMovie::Running().


新建一个Gui应用项目,名称为myMovie,然后添加下面界面效果的界面(在文章中最后),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);
    ui->label->setAutoFillBackground(true);

    movie = new QMovie(this);
    movie->setFileName("./love.gif");

    movie->setCacheMode(QMovie::CacheAll);

    QSize size = ui->label->size();
    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();
    QPixmap pix = movie->currentPixmap();
    pix.save(QString("./%1.png").arg(id));
    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

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>190</y>
      <width>80</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>开始</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>240</y>
      <width>80</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>停止</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>300</x>
      <y>190</y>
      <width>80</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>截屏</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_4">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>190</y>
      <width>80</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>暂停</string>
    </property>
   </widget>
   <widget class="QSlider" name="horizontalSlider">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>150</y>
      <width>331</width>
      <height>16</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
    <property name="tickPosition">
     <enum>QSlider::TicksBelow</enum>
    </property>
    <property name="tickInterval">
     <number>10</number>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>361</width>
      <height>121</height>
     </rect>
    </property>
    <property name="text">
     <string>Linux_Google</string>
    </property>
   </widget>
   <widget class="QWidget" name="layoutWidget">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>240</y>
      <width>123</width>
      <height>26</height>
     </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QLabel" name="label_2">
       <property name="text">
        <string>       进度</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QSpinBox" name="spinBox">
       <property name="suffix">
        <string>%</string>
       </property>
       <property name="maximum">
        <number>999</number>
       </property>
       <property name="value">
        <number>100</number>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

界面效果:




love.gif


                                                                                                                               文章出自:Linux_Google

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页