Qt平滑弹出页面

目标功能:

(1)按下btn,弹出绿色页面。

(2)按下btn2,绿色页面隐藏。

(3)按下左边余下的区域,绿色页面也隐藏。

(4)平滑地显示和隐藏

效果:

form.h

cpp 复制代码
#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Form *ui;
signals:
    void begin_move();
};

#endif // FORM_H

form.cpp

cpp 复制代码
#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
}

Form::~Form()
{
    delete ui;
}

void Form::on_pushButton_clicked()
{
    emit begin_move();
}

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "form.h"
#include <QPropertyAnimation>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
protected:
    void mousePressEvent(QMouseEvent *event) override;
    bool eventFilter(QObject *obj,QEvent *event) override;
private slots:
    void on_pushButton_clicked();
private:
    Ui::Widget *ui;
    Form *f;
    QPropertyAnimation *animation,*animation1;
    bool flag;
    bool isShow;
};
#endif // WIDGET_H

widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QEvent>

#pragma execution_character_set("utf-8")
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    f=new Form(this);
    f->hide();
    flag=1;
    isShow=0;
    ui->pushButton_2->installEventFilter(this);
    connect(f,&Form::begin_move,this,[=](){
        if(isShow == 1){
            animation1->start();
            isShow=0;
        }
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        qDebug()<<"按下了鼠标左键";
        qDebug()<<event->pos();
        if(isShow == 1){
            animation1->start();
            isShow=0;
        }
    }
}

bool Widget::eventFilter(QObject *obj, QEvent *event)
{
    if(obj==ui->pushButton_2){
        if(event->type() == QEvent::MouseButtonPress){
            qDebug()<<"按下按钮";
            QMouseEvent * me =static_cast<QMouseEvent *>(event);
            if(me->button() & Qt::LeftButton){
                qDebug()<<"按下左键";
                if(isShow == 1){
                    animation1->start();
                    isShow=0;
                }
            }
            return false;
        }
    }
    return QWidget::eventFilter(obj,event);
}

void Widget::on_pushButton_clicked()
{
    if(flag==1){
        flag=0;
        f->resize(300,height());
        f->move(width(),0);
        f->show();

        animation = new QPropertyAnimation(f,"pos");
        animation->setDuration(1000);  //设置动画完成的时间长度
        animation->setStartValue(QPoint(width(),0)); //设置动画的开始值
        animation->setEndValue(QPoint(width()-f->width(),0)); //设置动画的结束值

        animation1 = new QPropertyAnimation(f,"pos");
        animation1->setDuration(1000);  //设置动画完成的时间长度
        animation1->setStartValue(QPoint(width()-f->width(),0)); //设置动画的开始值
        animation1->setEndValue(QPoint(width(),0)); //设置动画的结束值
    }
    animation->start(); //启动动画
    isShow=1;
}
相关推荐
明洞日记1 天前
【设计模式手册013】命令模式 - 请求封装的优雅之道
java·设计模式·命令模式
阿波罗尼亚7 天前
Head First设计模式(六) 设计原则 命令模式
设计模式·命令模式
共享家95279 天前
QT-系统(文件)
开发语言·qt·命令模式
iFlow_AI11 天前
增强AI编程助手效能:使用开源Litho(deepwiki-rs)深度上下文赋能iFlow
人工智能·ai·ai编程·命令模式·iflow·iflow cli·心流ai助手
o0向阳而生0o14 天前
112、23种设计模式之命令模式(20/23)
设计模式·命令模式
郝学胜-神的一滴1 个月前
深入解析C++命令模式:设计原理与实际应用
开发语言·c++·程序人生·软件工程·命令模式
紫荆鱼1 个月前
设计模式-命令模式(Command)
c++·后端·设计模式·命令模式
杯莫停丶1 个月前
设计模式之:命令模式
设计模式·命令模式·1024程序员节·活动勋章
1379号监听员_1 个月前
嵌入式软件架构--显示界面架构(工厂流水线模型,HOME界面,命令界面)
stm32·单片机·架构·命令模式
太过平凡的小蚂蚁1 个月前
解耦的艺术:深入理解设计模式之命令模式
设计模式·命令模式