QPropertyAnimation QVBoxLayout 动画(一)

这里写自定义目录标题

QPropertyAnimation & QVBoxLayout

一、自定义动画属性
1.效果图
在这里插入图片描述
二、构建思路
垂直布局器加载自定义按钮,自定义按钮中封装动画属性。利用Q_PROPERTY(int fixHeight READ fixHeight WRITE setFixHeight)实现对按键高度的修改。

三、代码片段

#ifndef ANIMATIONBTN_H
#define ANIMATIONBTN_H

#include <QObject>
#include <QWidget>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QSizePolicy>
class animationBtn : public QPushButton
{
    Q_OBJECT
    Q_PROPERTY(int fixHeight READ fixHeight WRITE setFixHeight)

public:
    animationBtn(QWidget* par = nullptr);
    ~animationBtn();

    void start();
    void stop();
    int fixHeight();
    void setFixHeight(int h);

private slots:
    void onvalueChanged(QVariant value);
signals:
    void BtnHeight(int value);
private:
    bool m_stop;

};

#endif // ANIMATIONBTN_H




#include "animationbtn.h"
animationBtn::animationBtn(QWidget *par)
{
    resize(this->width(),30);
    this->setText("Button");
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    m_stop = false;
}


animationBtn::~animationBtn()
{

}

void animationBtn::start()
{
    m_stop = false;

    QPropertyAnimation *pAnimation = new QPropertyAnimation(this,"fixHeight");
    pAnimation->setStartValue(30);
    pAnimation->setEndValue(60);
    pAnimation->setDuration(2000);
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
    connect(pAnimation,&QPropertyAnimation::valueChanged,this,&animationBtn::onvalueChanged);
}

void animationBtn::stop()
{
    m_stop = true;
}

int animationBtn::fixHeight()
{
    return this->height();
}

void animationBtn::setFixHeight(int h)
{
    setFixedHeight(h);
}

void animationBtn::onvalueChanged(QVariant value)
{
    if(value == 30 && m_stop)
        return;
    if(value.toInt() == 60){
        QPropertyAnimation *pAnimation = new QPropertyAnimation(this,"fixHeight");
        pAnimation->setStartValue(60);
        pAnimation->setEndValue(30);
        pAnimation->setDuration(2000);
        pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
        connect(pAnimation,&QPropertyAnimation::valueChanged,this,&animationBtn::onvalueChanged);
    }
    else if(value.toInt() == 30){
        QPropertyAnimation *pAnimation = new QPropertyAnimation(this,"fixHeight");
        pAnimation->setStartValue(30);
        pAnimation->setEndValue(60);
        pAnimation->setDuration(2000);
        pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
        connect(pAnimation,&QPropertyAnimation::valueChanged,this,&animationBtn::onvalueChanged);
    }
    emit BtnHeight(value.toInt());
}

欢迎各方多多交流
QQ:519096571
e-mail:[email protected]

发布了30 篇原创文章 · 获赞 1 · 访问量 1168

猜你喜欢

转载自blog.csdn.net/u010906468/article/details/102825620