Qt工作笔记-Windows上界面滑动效果

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq78442761/article/details/84669697

运行截图如下:

源码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QMouseEvent;
class QPropertyAnimation;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

    void showWidget();
    void hideWidget();

private:
    Ui::Widget *ui;
    QPropertyAnimation *m_showAnimation;
    bool m_isAnimation;
    bool m_isShow;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QMetaProperty>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->leftWidget->setMaximumWidth(0);
    setMouseTracking(true);
    this->setWindowTitle("CSDN IT1995");

    m_showAnimation = new QPropertyAnimation(ui->leftWidget, "minimumWidth");

    m_isAnimation = false;
    m_isShow = false;
}

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


void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_isAnimation)
        return;

    qDebug()<< event->pos();

    if(event->pos().x() < 100 && !m_isShow){
        m_isShow = true;
        m_isAnimation = true;
        qDebug()<< "show";
        showWidget();
    }
    else if(event->pos().x() > 100 && m_isShow){
        m_isAnimation = true;
        qDebug()<< "hide";
        hideWidget();
        m_isShow = false;
    }

    m_isAnimation = false;
}

void Widget::showWidget()
{
    m_showAnimation->setDuration(1000);
    m_showAnimation->setStartValue(0);
    m_showAnimation->setEndValue(100);
    m_showAnimation->start();
}

void Widget::hideWidget()
{
    m_showAnimation->setDuration(1000);
    m_showAnimation->setStartValue(100);
    m_showAnimation->setEndValue(0);
    m_showAnimation->start();
}

 

展开阅读全文

没有更多推荐了,返回首页