qt实现轮播图,包含左右切换和选中切换。
该动画使用到串行动画和并行动画。(该文章包含全部代码)
大致思路:按钮滚动使用的串行动画,图片滚动使用的并行动画。
#ifndef UANIMAYION_H
#define UANIMAYION_H
#include <QWidget>
#include <QTimer>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
#include <QParallelAnimationGroup>
#include <QLabel>
#include <QPushButton>
#include <QButtonGroup>
#include <QHBoxLayout>
class Uanimayion : public QWidget
{
Q_OBJECT
public:
explicit Uanimayion(QWidget *parent = nullptr);
void animation(QString filename);
void loadbanner();
~Uanimayion();
void deleteAnimation();
signals:
private slots:
void showPictureSlot();
void slotbtnclick(int index);
void onBtnExpvalueChanged(const QVariant &variant);
void onBtnShrikvalueChanged(const QVariant &variant);
void slotswithclick();
private:
QPropertyAnimation* m_frontAnimation;
QPropertyAnimation* m_backAnimation;
QPropertyAnimation* m_btnExpAnimation;
QPropertyAnimation* m_btnShrikAnimation;
QParallelAnimationGroup* m_groupAnimation;
QSequentialAnimationGroup* sequentialGroup;
QTimer* m_qTimer;
int m_imgNumber;
int m_curIndex;
int m_preIndex;
bool m_blefttoright;
QStringList m_bannerInfo;
QLabel* page_front;
QLabel* page_back;
QPushButton* m_leftbtn;
QPushButton* m_rightbtn;
QButtonGroup* m_buttongroup;
};
#endif // UANIMAYION_H
#include "uanimayion.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
const int btn_expand_width = 24;//按钮拉伸宽度
const int btn_shrik_width = 6;//按钮收缩宽度
Uanimayion::Uanimayion(QWidget *parent) : QWidget(parent)
{
m_groupAnimation = NULL;
m_frontAnimation = NULL;
m_backAnimation = NULL;
m_btnExpAnimation = NULL;
m_btnShrikAnimation = NULL;
sequentialGroup = NULL;
m_qTimer = NULL;
m_imgNumber = 0;
m_preIndex = 0;
m_curIndex = 0;
m_blefttoright = false;
setAttribute(Qt::WA_TranslucentBackground);
this->setFixedSize(600,320);
m_buttongroup = new QButtonGroup(this);
page_front = new QLabel(this);
page_back = new QLabel(this);
page_front->setFixedSize(600,320);
page_back->setFixedSize(600,320);
page_front->move(0,0);
page_back->move(0,0);
page_front-