Header:
#ifndef ANIM_H
#define ANIM_H
#include <QWidget>
#include <QString>
#include <QTimer>
#include <QLabel>
#include <QList>
#include <QPixmap>
class AnimatedLabel
: public QLabel{
Q_OBJECT
public:
AnimatedLabel
(const QString & image,
const int imageCount,
QWidget* parent
= 0);
private slots:
void changeImage();
private:
QList<QPixmap> pixmaps;
int currentPixmap;
};
#endif // ANIM_H
#ifndef ANIM_H
#define ANIM_H
#include <QWidget>
#include <QString>
#include <QTimer>
#include <QLabel>
#include <QList>
#include <QPixmap>
class AnimatedLabel : public QLabel
{
Q_OBJECT
public:
AnimatedLabel(const QString & image, const int imageCount, QWidget* parent = 0);
private slots:
void changeImage();
private:
QList<QPixmap> pixmaps;
int currentPixmap;
QTimer timer;
};
#endif // ANIM_H
To copy to clipboard, switch view to plain text mode
and source:
#include "anim.h"
#include <QApplication>
#include <QImage>
AnimatedLabel
::AnimatedLabel(const QString &image,
const int imageCount,
QWidget *parent
) : QLabel(parent
), currentPixmap
(0) {
img.load(image);
int subImageHeight = img.height() / imageCount;
for (int i = 0; i < imageCount; i++)
{
QImage subImage
= img.
copy(0, i
* subImageHeight, img.
width(), subImageHeight
);
pixmaps.
push_back(QPixmap::fromImage(subImage
));
}
connect(&timer, SIGNAL(timeout()), SLOT(changeImage()));
timer.start(100);
changeImage();
}
void AnimatedLabel::changeImage()
{
if (currentPixmap >= pixmaps.length())
currentPixmap = 0;
setPixmap(pixmaps.at(currentPixmap));
currentPixmap++;
}
int main(int argc, char *argv[])
{
AnimatedLabel bt("process-working.png", 8);
bt.show();
return a.exec();
}
#include "anim.h"
#include <QApplication>
#include <QImage>
AnimatedLabel::AnimatedLabel(const QString &image, const int imageCount, QWidget *parent)
: QLabel(parent), currentPixmap(0)
{
QImage img;
img.load(image);
int subImageHeight = img.height() / imageCount;
for (int i = 0; i < imageCount; i++)
{
QImage subImage = img.copy(0, i * subImageHeight, img.width(), subImageHeight);
pixmaps.push_back(QPixmap::fromImage(subImage));
}
connect(&timer, SIGNAL(timeout()), SLOT(changeImage()));
timer.start(100);
changeImage();
}
void AnimatedLabel::changeImage()
{
if (currentPixmap >= pixmaps.length())
currentPixmap = 0;
setPixmap(pixmaps.at(currentPixmap));
currentPixmap++;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AnimatedLabel bt("process-working.png", 8);
bt.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks