之前看到有人用 QGraphicsView实现了轮播图的效果,想着能不能简易一点用qt的动画类实现呢?
先上效果图:
效果还行,直接上代码吧,代码如下。大家可以根据自己的需求完善一下切换的point和线性动画属性。
MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
QGraphicsView
}
void MainWindow::slot_statechange1(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
if(newState ==QAbstractAnimation::State::Running && oldState ==QAbstractAnimation::State::Stopped)
{
ui->label_1->raise(); //label置顶
}
else if(newState ==QAbstractAnimation::State::Stopped && oldState ==QAbstractAnimation::State::Running)
{
ui->label_2->lower();//label下移
ui->label_3->lower();
}
}
void MainWindow::slot_statechange2(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
if(newState ==QAbstractAnimation::State::Running && oldState ==QAbstractAnimation::State::Stopped)
{
ui->label_2->raise();
}
else if(newState ==QAbstractAnimation::State::Stopped && oldState ==QAbstractAnimation::State::Running)
{
ui->label_3->lower();
ui->label_1->lower();
}
}
void MainWindow::slot_statechange3(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
if(newState ==QAbstractAnimation::State::Running && oldState ==QAbstractAnimation::State::Stopped)
{
ui->label_3->raise();
}
else if(newState ==QAbstractAnimation::State::Stopped && oldState ==QAbstractAnimation::State::Running)
{
ui->label_1->lower();
ui->label_2->lower();
}
}
void MainWindow::on_pushButton_clicked()
{
const QRect mid (120, 0, 450,140);
const QRect right_0(240, 0, 450,140);
const QRect right_1(286, 14, 405,126);
const QRect left_0 (0, 14, 405,126);
const QRect left_1 (0, 0, 450, 140);
const int stoptime = 4500; //中间暂停时间
//--------------
QPropertyAnimation *animation_label_1_0 = new QPropertyAnimation(ui->label_1, "geometry");
QPropertyAnimation *animation_label_1_1 = new QPropertyAnimation(ui->label_1, "geometry");
QPropertyAnimation *animation_label_1_2 = new QPropertyAnimation(ui->label_1, "geometry");
QPropertyAnimation *animation_label_1_3 = new QPropertyAnimation(ui->label_1, "geometry");
QPropertyAnimation *animation_label_1_4 = new QPropertyAnimation(ui->label_1, "geometry");
//此信号槽用于控件的前后顺序
connect(animation_label_1_4,&QPropertyAnimation::stateChanged, this ,&MainWindow::slot_statechange1);
animation_label_1_0->setDuration(1000);
animation_label_1_0->setStartValue(mid);
animation_label_1_0->setEndValue(right_0);
animation_label_1_0->setLoopCount(1);
animation_label_1_1->setDuration(1000);
animation_label_1_1->setStartValue(right_0);
animation_label_1_1->setEndValue(right_1);
animation_label_1_1->setLoopCount(1);
animation_label_1_2->setDuration(2000);
animation_label_1_2->setStartValue(right_1);
animation_label_1_2->setEndValue(left_0);
animation_label_1_2->setLoopCount(1);
animation_label_1_3->setDuration(1000);
animation_label_1_3->setStartValue(left_0);
animation_label_1_3->setEndValue(left_1);
animation_label_1_3->setLoopCount(1);
animation_label_1_4->setDuration(1000);
animation_label_1_4->setStartValue(left_1);
animation_label_1_4->setEndValue(mid);
animation_label_1_4->setLoopCount(1);
animation_label_1_0->setEasingCurve(QEasingCurve::OutSine); //动画类型
animation_label_1_1->setEasingCurve(QEasingCurve::OutSine);
animation_label_1_2->setEasingCurve(QEasingCurve::OutSine);
animation_label_1_3->setEasingCurve(QEasingCurve::OutSine);
animation_label_1_4->setEasingCurve(QEasingCurve::OutSine);
QSequentialAnimationGroup *pPosGroup_label_1 = new QSequentialAnimationGroup(this);
pPosGroup_label_1->addAnimation(animation_label_1_0);
pPosGroup_label_1->addAnimation(animation_label_1_1);
pPosGroup_label_1->addPause(stoptime);
pPosGroup_label_1->addAnimation(animation_label_1_2);
pPosGroup_label_1->addPause(stoptime);
pPosGroup_label_1->addAnimation(animation_label_1_3);
pPosGroup_label_1->addAnimation(animation_label_1_4);
pPosGroup_label_1->addPause(stoptime);
pPosGroup_label_1->setLoopCount(1);
//--------------------
QPropertyAnimation *animation_label_2_0 = new QPropertyAnimation(ui->label_2, "geometry");
QPropertyAnimation *animation_label_2_1 = new QPropertyAnimation(ui->label_2, "geometry");
QPropertyAnimation *animation_label_2_2 = new QPropertyAnimation(ui->label_2, "geometry");
QPropertyAnimation *animation_label_2_3 = new QPropertyAnimation(ui->label_2, "geometry");
QPropertyAnimation *animation_label_2_4 = new QPropertyAnimation(ui->label_2, "geometry");
connect(animation_label_2_1,&QPropertyAnimation::stateChanged, this ,&MainWindow::slot_statechange2);
animation_label_2_0->setDuration(1000);
animation_label_2_0->setStartValue(left_0);
animation_label_2_0->setEndValue(left_1);
animation_label_2_0->setLoopCount(1);
animation_label_2_1->setDuration(1000);
animation_label_2_1->setStartValue(left_1);
animation_label_2_1->setEndValue(mid);
animation_label_2_1->setLoopCount(1);
animation_label_2_2->setDuration(1000);
animation_label_2_2->setStartValue(mid);
animation_label_2_2->setEndValue(right_0);
animation_label_2_2->setLoopCount(1);
animation_label_2_3->setDuration(1000);
animation_label_2_3->setStartValue(right_0);
animation_label_2_3->setEndValue(right_1);
animation_label_2_3->setLoopCount(1);
animation_label_2_4->setDuration(2000);
animation_label_2_4->setStartValue(right_1);
animation_label_2_4->setEndValue(left_0);
animation_label_2_4->setLoopCount(1);
animation_label_2_0->setEasingCurve(QEasingCurve::OutSine);
animation_label_2_1->setEasingCurve(QEasingCurve::OutSine);
animation_label_2_2->setEasingCurve(QEasingCurve::OutSine);
animation_label_2_3->setEasingCurve(QEasingCurve::OutSine);
animation_label_2_4->setEasingCurve(QEasingCurve::OutSine);
QSequentialAnimationGroup *pPosGroup_label_2 = new QSequentialAnimationGroup(this);
pPosGroup_label_2->addAnimation(animation_label_2_0);
pPosGroup_label_2->addAnimation(animation_label_2_1);
pPosGroup_label_2->addPause(stoptime);
pPosGroup_label_2->addAnimation(animation_label_2_2);
pPosGroup_label_2->addAnimation(animation_label_2_3);
pPosGroup_label_2->addPause(stoptime);
pPosGroup_label_2->addAnimation(animation_label_2_4);
pPosGroup_label_2->addPause(stoptime);
pPosGroup_label_2->setLoopCount(1);
//---------------
QPropertyAnimation *animation_label_3_0 = new QPropertyAnimation(ui->label_3, "geometry");
QPropertyAnimation *animation_label_3_1 = new QPropertyAnimation(ui->label_3, "geometry");
QPropertyAnimation *animation_label_3_2 = new QPropertyAnimation(ui->label_3, "geometry");
QPropertyAnimation *animation_label_3_3 = new QPropertyAnimation(ui->label_3, "geometry");
QPropertyAnimation *animation_label_3_4 = new QPropertyAnimation(ui->label_3, "geometry");
connect(animation_label_3_2,&QPropertyAnimation::stateChanged, this ,&MainWindow::slot_statechange3);
animation_label_3_0->setDuration(2000);
animation_label_3_0->setStartValue(right_1);
animation_label_3_0->setEndValue(left_0);
animation_label_3_0->setLoopCount(1);
animation_label_3_1->setDuration(1000);
animation_label_3_1->setStartValue(left_0);
animation_label_3_1->setEndValue(left_1);
animation_label_3_1->setLoopCount(1);
animation_label_3_2->setDuration(1000);
animation_label_3_2->setStartValue(left_1);
animation_label_3_2->setEndValue(mid);
animation_label_3_2->setLoopCount(1);
animation_label_3_3->setDuration(1000);
animation_label_3_3->setStartValue(mid);
animation_label_3_3->setEndValue(right_0);
animation_label_3_3->setLoopCount(1);
animation_label_3_4->setDuration(1000);
animation_label_3_4->setStartValue(right_0);
animation_label_3_4->setEndValue(right_1);
animation_label_3_4->setLoopCount(1);
animation_label_3_0->setEasingCurve(QEasingCurve::OutSine);
animation_label_3_1->setEasingCurve(QEasingCurve::OutSine);
animation_label_3_2->setEasingCurve(QEasingCurve::OutSine);
animation_label_3_3->setEasingCurve(QEasingCurve::OutSine);
animation_label_3_4->setEasingCurve(QEasingCurve::OutSine);
QSequentialAnimationGroup *pPosGroup_label_3 = new QSequentialAnimationGroup(this);
pPosGroup_label_3->addAnimation(animation_label_3_0);
pPosGroup_label_3->addPause(stoptime);
pPosGroup_label_3->addAnimation(animation_label_3_1);
pPosGroup_label_3->addAnimation(animation_label_3_2);
pPosGroup_label_3->addPause(stoptime);
pPosGroup_label_3->addAnimation(animation_label_3_3);
pPosGroup_label_3->addAnimation(animation_label_3_4);
pPosGroup_label_3->addPause(stoptime);
pPosGroup_label_3->setLoopCount(1);
QParallelAnimationGroup *m_group;
m_group = new QParallelAnimationGroup(this);
m_group->addAnimation(pPosGroup_label_1);
m_group->addAnimation(pPosGroup_label_2);
m_group->addAnimation(pPosGroup_label_3);
m_group->setDirection(QAbstractAnimation::Forward);
m_group->setLoopCount(-1); //无限循环
m_group->start();
}
工程已上传:https://download.youkuaiyun.com/download/PepperEggFriedRice/12593588