先看效果
UI界面
.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void slotShowWidget();
private:
Ui::Widget *ui;
bool _showFlg; //是否显示了下半部分窗口
};
#endif // WIDGET_H
.cpp
#include "widget.h"
#include <QDebug>
#include "ui_widget.h"
#include <QPropertyAnimation>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),
_showFlg(false)
{
ui->setupUi(this);
this->setFixedHeight(100); //设置下半部分窗口隐藏
connect(ui->btnScale, &QPushButton::clicked, this, &Widget::slotShowWidget);
}
Widget::~Widget()
{
delete ui;
}
void Widget::slotShowWidget()
{
QPropertyAnimation* animation = new QPropertyAnimation(this, ""); //qt动画类
animation->setDuration(500); //动画持续时间500ms
if(!_showFlg){
ui->btnScale->setIcon(QIcon(tr(":/Image/ArrowUp.png")));
animation->setStartValue(100);//动画开始值和结束值
animation->setEndValue(500);
connect(animation, &QPropertyAnimation::valueChanged, [this](const QVariant& value){
this->setFixedHeight(value.toInt());
qDebug()<<value.toInt();
});
animation->start(QAbstractAnimation::DeleteWhenStopped);
_showFlg = true;
}
else {
ui->btnScale->setIcon(QIcon(":/Image/ArrowDown.png"));
animation->setStartValue(500);
animation->setEndValue(100);
connect(animation, &QPropertyAnimation::valueChanged, [this](const QVariant& value){
this->setFixedHeight(value.toInt());
});
animation->start(QAbstractAnimation::DeleteWhenStopped);
_showFlg = false;
}
}