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

运行截图如下:

Qt工作笔记-Windows上界面滑动效果_第1张图片

源码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

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 

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 
#include 
#include 
#include 

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();
}

 

你可能感兴趣的:(C/C++,Qt,工作笔记)