Help us understand the problem. What is going on with this article?

【C++】QtでGifアニメーションを表示する方法

■環境

 - PC : Windows10
 - C++
 - Qt 5.12.6(ソースコードからビルド)

※WindowsでQtを使えるようになるまでの話は下記記事に書いています
【Windows 10にQtをインストール】
https://qiita.com/tomtum/items/15c8b6485dc80921ff72

■コード

Gifアニメーションを、
 ・枠なしウィンドウ
 ・等倍
 ・3秒間
で表示する事を目標にします

main.cpp
#include <iostream>
#include <string>
#include "QtWidgets/QApplication"
#include "QtGui/QMovie"
#include "QtWidgets/QLabel"
#include "QtCore/QTimer"
#include "QtCore/Qt"

//Gifファイルを表示するための自作クラス
class MyQtClass{

public:

  MyQtClass(std::string f, int w, int h){
    fname = f;
    width = w;
    height = h;
    movie = nullptr;
    label = nullptr;
    sleeplength = 3000;
    flag = true;
  }

  ~MyQtClass() {
    if(flag){
      movie->stop();
      label->clear();
      delete movie;
      delete label;
    }
  }

  bool ShowGifImage() {
    SetMovieAndLabel();
    if(!flag){
      return flag;
    }
    //Gif再生
    movie->start();
    //描画
    label->show();
    //sleeplength[msec]経過したら描画終了
    QTimer::singleShot(sleeplength, label, SLOT(close()));
    flag = true;
    return flag;
  }

private:
  QMovie *movie;
  QLabel *label;
  std::string fname;
  int width;
  int height;
  int sleeplength;
  bool flag;

  void SetMovieAndLabel() {
    try{
      //Gifファイル格納
      movie = new QMovie(fname.c_str());
      //描画の設定
      label = new QLabel(nullptr);
      label->resize(width, height);  //描画サイズ
      label->setMovie(movie);        //描画対象
      label->setWindowFlags(Qt::FramelessWindowHint);  //フレームなし
      flag = true;
    }
    catch(std::bad_alloc& e){
      delete movie;
      delete label;
      flag = false;
    }
  }
};

int main(int argc, char**argv) {

  QApplication app(argc, argv);

  //描画の準備
  std::string gifname = "animation.gif";
  MyQtClass mqc2(gifname, 800, 480); //入力画像の横×縦を指定

  //描画
  bool flag = mqc2.ShowGifImage();
  if(!flag){
    app.exit();
    return -1;
  }

  return app.exec();
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away