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:
- <?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>
注:mainwindow.ui不能直接在qt中改,以gedit打开,粘贴,然后重新打开工程即可
后两张为gif