当前位置:   article > 正文

《Qt5+播放gif动图》_qt5放gif图片

qt5放gif图片

示例代码演示了如何使用QMovieQLabel来显示动画(GIF)。如果想要播放简单的动画而没有安装和部署多媒体框架的复杂性,QMovie最有用。

 

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. class QCheckBox;
  5. class QGridLayout;
  6. class QHBoxLayout;
  7. class QLabel;
  8. class QMovie;
  9. class QSlider;
  10. class QSpinBox;
  11. class QToolButton;
  12. class QVBoxLayout;
  13. namespace Ui {
  14. class Widget;
  15. }
  16. class Widget : public QWidget
  17. {
  18. Q_OBJECT
  19. public:
  20. explicit Widget(QWidget *parent = 0);
  21. ~Widget();
  22. void openFile(const QString &fileName);
  23. private slots:
  24. void open();
  25. void goToFrame(int frame);
  26. void fitToWindow();
  27. void updateButtons();
  28. void updateFrameSlider();
  29. private:
  30. Ui::Widget *ui;
  31. private:
  32. void createControls();
  33. void createButtons();
  34. QString currentMovieDirectory;
  35. QLabel *movieLabel;
  36. QMovie *movie;
  37. QToolButton *openButton;
  38. QToolButton *playButton;
  39. QToolButton *pauseButton;
  40. QToolButton *stopButton;
  41. QToolButton *quitButton;
  42. QCheckBox *fitCheckBox;
  43. QSlider *frameSlider;
  44. QSpinBox *speedSpinBox;
  45. QLabel *frameLabel;
  46. QLabel *speedLabel;
  47. QGridLayout *controlsLayout;
  48. QHBoxLayout *buttonsLayout;
  49. QVBoxLayout *mainLayout;
  50. };
  51. #endif // WIDGET_H

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QtWidgets>
  4. Widget::Widget(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::Widget)
  7. {
  8. ui->setupUi(this);
  9. movie = new QMovie(this);
  10. movie->setCacheMode(QMovie::CacheAll);
  11. movieLabel = new QLabel(tr("No movie loaded"));
  12. movieLabel->setAlignment(Qt::AlignCenter);
  13. movieLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  14. movieLabel->setBackgroundRole(QPalette::Dark);
  15. movieLabel->setAutoFillBackground(true);
  16. currentMovieDirectory = "movies";
  17. createControls();
  18. createButtons();
  19. connect(movie, SIGNAL(frameChanged(int)), this, SLOT(updateFrameSlider()));
  20. connect(movie, SIGNAL(stateChanged(QMovie::MovieState)),
  21. this, SLOT(updateButtons()));
  22. connect(fitCheckBox, SIGNAL(clicked()), this, SLOT(fitToWindow()));
  23. connect(frameSlider, SIGNAL(valueChanged(int)), this, SLOT(goToFrame(int)));
  24. connect(speedSpinBox, SIGNAL(valueChanged(int)),
  25. movie, SLOT(setSpeed(int)));
  26. mainLayout = new QVBoxLayout;
  27. mainLayout->addWidget(movieLabel);
  28. mainLayout->addLayout(controlsLayout);
  29. mainLayout->addLayout(buttonsLayout);
  30. setLayout(mainLayout);
  31. updateFrameSlider();
  32. updateButtons();
  33. setWindowTitle(tr("Movie Player"));
  34. resize(400, 400);
  35. }
  36. Widget::~Widget()
  37. {
  38. delete ui;
  39. }
  40. void Widget::open()
  41. {
  42. QString fileName = QFileDialog::getOpenFileName(this, tr("Open a Movie"),
  43. currentMovieDirectory);
  44. if (!fileName.isEmpty())
  45. openFile(fileName);
  46. }
  47. void Widget::openFile(const QString &fileName)
  48. {
  49. currentMovieDirectory = QFileInfo(fileName).path();
  50. movie->stop();
  51. movieLabel->setMovie(movie);
  52. movie->setFileName(fileName);
  53. movie->start();
  54. updateFrameSlider();
  55. updateButtons();
  56. }
  57. void Widget::goToFrame(int frame)
  58. {
  59. movie->jumpToFrame(frame);
  60. }
  61. void Widget::fitToWindow()
  62. {
  63. movieLabel->setScaledContents(fitCheckBox->isChecked());
  64. }
  65. void Widget::updateFrameSlider()
  66. {
  67. bool hasFrames = (movie->currentFrameNumber() >= 0);
  68. if (hasFrames) {
  69. if (movie->frameCount() > 0) {
  70. frameSlider->setMaximum(movie->frameCount() - 1);
  71. } else {
  72. if (movie->currentFrameNumber() > frameSlider->maximum())
  73. frameSlider->setMaximum(movie->currentFrameNumber());
  74. }
  75. frameSlider->setValue(movie->currentFrameNumber());
  76. } else {
  77. frameSlider->setMaximum(0);
  78. }
  79. frameLabel->setEnabled(hasFrames);
  80. frameSlider->setEnabled(hasFrames);
  81. }
  82. void Widget::updateButtons()
  83. {
  84. playButton->setEnabled(movie->isValid() && movie->frameCount() != 1
  85. && movie->state() == QMovie::NotRunning);
  86. pauseButton->setEnabled(movie->state() != QMovie::NotRunning);
  87. pauseButton->setChecked(movie->state() == QMovie::Paused);
  88. stopButton->setEnabled(movie->state() != QMovie::NotRunning);
  89. }
  90. void Widget::createControls()
  91. {
  92. fitCheckBox = new QCheckBox(tr("Fit to Window"));
  93. frameLabel = new QLabel(tr("Current frame:"));
  94. frameSlider = new QSlider(Qt::Horizontal);
  95. frameSlider->setTickPosition(QSlider::TicksBelow);
  96. frameSlider->setTickInterval(10);
  97. speedLabel = new QLabel(tr("Speed:"));
  98. speedSpinBox = new QSpinBox;
  99. speedSpinBox->setRange(1, 9999);
  100. speedSpinBox->setValue(100);
  101. speedSpinBox->setSuffix(tr("%"));
  102. controlsLayout = new QGridLayout;
  103. controlsLayout->addWidget(fitCheckBox, 0, 0, 1, 2);
  104. controlsLayout->addWidget(frameLabel, 1, 0);
  105. controlsLayout->addWidget(frameSlider, 1, 1, 1, 2);
  106. controlsLayout->addWidget(speedLabel, 2, 0);
  107. controlsLayout->addWidget(speedSpinBox, 2, 1);
  108. }
  109. void Widget::createButtons()
  110. {
  111. QSize iconSize(36, 36);
  112. openButton = new QToolButton;
  113. openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
  114. openButton->setIconSize(iconSize);
  115. openButton->setToolTip(tr("Open File"));
  116. connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
  117. playButton = new QToolButton;
  118. playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
  119. playButton->setIconSize(iconSize);
  120. playButton->setToolTip(tr("Play"));
  121. connect(playButton, SIGNAL(clicked()), movie, SLOT(start()));
  122. pauseButton = new QToolButton;
  123. pauseButton->setCheckable(true);
  124. pauseButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
  125. pauseButton->setIconSize(iconSize);
  126. pauseButton->setToolTip(tr("Pause"));
  127. connect(pauseButton, SIGNAL(clicked(bool)), movie, SLOT(setPaused(bool)));
  128. stopButton = new QToolButton;
  129. stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
  130. stopButton->setIconSize(iconSize);
  131. stopButton->setToolTip(tr("Stop"));
  132. connect(stopButton, SIGNAL(clicked()), movie, SLOT(stop()));
  133. quitButton = new QToolButton;
  134. quitButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
  135. quitButton->setIconSize(iconSize);
  136. quitButton->setToolTip(tr("Quit"));
  137. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
  138. buttonsLayout = new QHBoxLayout;
  139. buttonsLayout->addStretch();
  140. buttonsLayout->addWidget(openButton);
  141. buttonsLayout->addWidget(playButton);
  142. buttonsLayout->addWidget(pauseButton);
  143. buttonsLayout->addWidget(stopButton);
  144. buttonsLayout->addWidget(quitButton);
  145. buttonsLayout->addStretch();
  146. }

 

完整代码


 

 

链接:https://pan.baidu.com/s/17yFaP0_yutI7NcNYmvGwBA 
提取码:q182 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/297516
推荐阅读
相关标签
  

闽ICP备14008679号