2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-01-09

■環境

 - 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 : private QMovie, private QLabel {

public:

	MyQtClass(const std::string f, int w, int h) {
		Initialize(f, w, h);
	}

	~MyQtClass() {
		StopMovie();
		QLabel::clear();
	}

	void ShowGifImage() {
		QMovie::start();
		QLabel::show();
	}

private:

	void Initialize(const std::string f, const int w, const int h) {
		QMovie::setFileName(f.c_str());
		QLabel::resize(w, h);
		QLabel::setMovie(this);
		QLabel::setWindowFlags(Qt::FramelessWindowHint);
	}

	void StopMovie() {
		QMovie::stop();
	}
};

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

	QApplication app(argc, argv);

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

	//描画
	mqc2.ShowGifImage();
	QTimer::singleShot(3000, &app, SLOT(quit()));

	return app.exec();
}

2
2
14

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?