qt实现交换的动画效果

在这里插入图片描述
.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLineEdit>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void CreateBtnWgt();

private slots:
    void onAnimationClicked();

private:
   void changeBtnPostion(QPushButton *btn1,QPushButton *btn2);

private:

    QLineEdit *line1=nullptr;
    QLineEdit *line2=nullptr;
    QLineEdit *line3=nullptr;
    QLineEdit *line4=nullptr;
    QLineEdit *line5=nullptr;

    QPushButton *btn1=nullptr;
    QPushButton *btn2=nullptr;
    QPushButton *btn3=nullptr;
    QPushButton *btn4=nullptr;
    QPushButton *btn5=nullptr;
    QWidget *btnWgt=nullptr;
};
#endif // WIDGET_H

.cpp

#include "widget.h"
#include <iostream>
#include <qdebug.h>
#include <QHBoxLayout>
#include <QPropertyAnimation>
#include <QValidator>
#include <QTimer>
#include <QTime>
#include <thread>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    CreateBtnWgt();
    QWidget *titleWgt=new QWidget(this);
    QPushButton *animationBtn=new QPushButton(tr("Sort"),this);
    animationBtn->setFixedSize(80,27);
    QHBoxLayout *titleLayout=new QHBoxLayout(this);
    line1=new QLineEdit(this);
    line2=new QLineEdit(this);
    line3=new QLineEdit(this);
    line4=new QLineEdit(this);
    line5=new QLineEdit(this);
    line1->setValidator(new QIntValidator());
    line2->setValidator(new QIntValidator());
    line3->setValidator(new QIntValidator());
    line4->setValidator(new QIntValidator());
    line5->setValidator(new QIntValidator());
    line1->setFixedWidth(100);
    line2->setFixedWidth(100);
    line3->setFixedWidth(100);
    line4->setFixedWidth(100);
    line5->setFixedWidth(100);
    titleLayout->addWidget(line1);
    titleLayout->addWidget(line2);
    titleLayout->addWidget(line3);
    titleLayout->addWidget(line4);
    titleLayout->addWidget(line5);
    titleLayout->addWidget(animationBtn);
    titleLayout->setMargin(5);
    titleLayout->setSpacing(5);
    titleWgt->setLayout(titleLayout);
    titleWgt->setFixedHeight(50);
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(titleWgt);
    mainLayout->addWidget(btnWgt);
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    this->setLayout(mainLayout);
    connect(animationBtn,&QPushButton::clicked,this,&Widget::onAnimationClicked);

    this->resize(800,600);

    //qDebug()<<"ans::"<<ans;
}

Widget::~Widget()
{
}

void Widget::CreateBtnWgt()
{
    QFont textFont;
    textFont.setPointSize(20);
    textFont.setFamily("Microsoft YaHei");
    btn1=new QPushButton(this);
    btn2=new QPushButton(this);
    btn3=new QPushButton(this);
    btn4=new QPushButton(this);
    btn5=new QPushButton(this);

    btn1->setFixedSize(100,150);
    btn2->setFixedSize(100,150);
    btn3->setFixedSize(100,150);
    btn4->setFixedSize(100,150);
    btn5->setFixedSize(100,150);

    btn1->setStyleSheet("border-radius:5px;background-color:rgb(69,133,254);");
    btn2->setStyleSheet("border-radius:5px;background-color:rgb(69,133,254);");
    btn3->setStyleSheet("border-radius:5px;background-color:rgb(69,133,254);");
    btn4->setStyleSheet("border-radius:5px;background-color:rgb(69,133,254);");
    btn5->setStyleSheet("border-radius:5px;background-color:rgb(69,133,254);");

    btn1->setFont(textFont);
    btn2->setFont(textFont);
    btn3->setFont(textFont);
    btn4->setFont(textFont);
    btn5->setFont(textFont);

    //btn1->move(50,250);

    btnWgt=new QWidget(this);
    QHBoxLayout *btnLayout=new QHBoxLayout(this);
    btnLayout->addWidget(btn1);
    btnLayout->addWidget(btn2);
    btnLayout->addWidget(btn3);
    btnLayout->addWidget(btn4);
    btnLayout->addWidget(btn5);
    btnLayout->setMargin(20);
    btnLayout->setSpacing(50);
    btnWgt->setLayout(btnLayout);

}

void Widget::onAnimationClicked()
{
    btn1->setText(line1->text());
    btn2->setText(line2->text());
    btn3->setText(line3->text());
    btn4->setText(line4->text());
    btn5->setText(line5->text());

    changeBtnPostion(btn1,btn5);



}

void Widget::changeBtnPostion(QPushButton *bt1, QPushButton *bt2)
{
    QPropertyAnimation *animation1 = new QPropertyAnimation(bt1, "geometry");

    int x1=bt1->pos().x();
    int y1=bt1->pos().y();

    animation1->setDuration(500);
    animation1->setStartValue(QRect(bt1->pos().x(),bt1->pos().y(), bt1->width(), bt1->height()));
    animation1->setEndValue(QRect(bt1->pos().x(),bt1->pos().y()+200, bt1->width(), bt1->height()));
    animation1->setEasingCurve(QEasingCurve::InCurve);   //    OutBounce
    animation1->start();

    int x2=bt2->pos().x();
    int y2=bt2->pos().y();

    QPropertyAnimation *animation2 = new QPropertyAnimation(bt2, "geometry");
    animation2->setDuration(500);
    animation2->setStartValue(QRect(bt2->pos().x(),bt2->pos().y(), bt2->width(), bt2->height()));
    animation2->setEndValue(QRect(x1,y1, bt2->width(), bt2->height()));
    animation2->setEasingCurve(QEasingCurve::InCurve);   //    OutBounce
    animation2->start();

    QPropertyAnimation *animation3 = new QPropertyAnimation(bt1, "geometry");
    animation3->setDuration(500);
    animation3->setStartValue(QRect(bt1->pos().x(),bt1->pos().y(), bt1->width(), bt1->height()));
    animation3->setEndValue(QRect(x2,bt1->pos().y(), bt1->width(), bt1->height()));
    animation3->setEasingCurve(QEasingCurve::InCurve);   //    OutBounce
    animation3->start();

    QPropertyAnimation *animation4 = new QPropertyAnimation(bt1, "geometry");
    animation4->setDuration(500);
    animation4->setStartValue(QRect(bt1->pos().x(),bt1->pos().y(), bt1->width(), bt1->height()));
    animation4->setEndValue(QRect(x2,y2, bt1->width(), bt1->height()));
    animation4->setEasingCurve(QEasingCurve::InCurve);   //    OutBounce
    animation4->start();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油小杜(接qt定制功能,单模块开发等)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值