QT-Qt显示GIF图片

相关资料:

https://blog.csdn.net/kidults/article/details/80195524

 

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QPainter>
 6 
 7 #include "gifWidget.h"
 8 
 9 QT_BEGIN_NAMESPACE
10 namespace Ui { class MainWindow; }
11 QT_END_NAMESPACE
12 
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16 
17 public:
18     MainWindow(QWidget *parent = nullptr);
19     ~MainWindow();
20     void paintEvent(QPaintEvent* pEvent);
21 
22 private:
23     Ui::MainWindow *ui;
24 
25     gifWidget * m_pgifWidget;
26 };
27 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

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

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

    setWindowTitle(QStringLiteral("Qt显示GIF图片"));

    m_pgifWidget = new gifWidget(this);
    m_pgifWidget->setGeometry(100,200, 400,300);
}

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

void MainWindow::paintEvent(QPaintEvent *pEvent)
{
    // 界面背景图片片
    QPainter painter(this);
    painter.drawPixmap(0, 0, this->width(), this->height(), QPixmap(":/new/prefix1/q.jpg"));
}
View Code

gifWidget.h

 1 #ifndef GIFWIDGET_H
 2 #define GIFWIDGET_H
 3 
 4 #include <QWidget>
 5 #include <QLabel>
 6 #include <QPushButton>
 7 #include <QVBoxLayout>
 8 #include <QMovie>
 9 
10 class gifWidget : public QWidget
11 {
12     Q_OBJECT
13 
14 public:
15     explicit gifWidget(QWidget *parent = 0);
16     ~gifWidget();
17 
18 private:
19     QMovie *movie;
20     QLabel *label;
21     bool isPlaying;
22     QPushButton *button;
23     QPushButton *button2;
24 private slots:
25     void playGif();
26 };
27 
28 #endif // GIFWIDGET_H
View Code

gifWidget.cpp

 1 #include "gifWidget.h"
 2 
 3 gifWidget::gifWidget(QWidget *parent) :
 4     QWidget(parent)
 5 {
 6     isPlaying = false;
 7 
 8     movie = new QMovie(":/new/prefix1/A.gif");
 9     label = new QLabel(this);
10     button = new QPushButton(this);
11 
12     button->setText(QStringLiteral("播放"));
13     connect(button,SIGNAL(clicked(bool)),this,SLOT(playGif()));
14 
15     button2 = new QPushButton(this);
16     button2->setGeometry(button->x(), button->y()+100, button2->width(), button2->height());
17 
18     QVBoxLayout *hLayout = new QVBoxLayout(this);
19     hLayout->addWidget(button);
20     hLayout->addWidget(label);
21     setLayout(hLayout);
22 }
23 
24 gifWidget::~gifWidget()
25 {
26 
27 }
28 
29 void gifWidget::playGif()
30 {
31     if(isPlaying){
32         movie->stop();
33         label->clear();    //stop的时候把label清空
34         isPlaying = false;
35         button->setText(QStringLiteral("播放"));
36     }else{
37         label->setMovie(movie); //为label设置movie
38         movie->start();         //开始显示
39         isPlaying = true;
40         button->setText(QStringLiteral("停止"));
41     }
42 }
View Code

 

posted on 2020-07-17 15:50  疯狂delphi  阅读(399)  评论(0编辑  收藏  举报

导航