当前位置:   article > 正文

Qt窗口动画-淡入淡出-移动-缩放_qt窗口无法移动到屏幕外

qt窗口无法移动到屏幕外

SubWidget.h

#ifndef SUBWIDGET_H
#define SUBWIDGET_H
#include <QPainter>
#include <QWidget>
#include <QPaintEvent>
#include <QStyleOption>
class SubWidget : public QWidget
{
    Q_OBJECT
public:
    SubWidget(QWidget *parent = nullptr);
    ~SubWidget();
protected:
    void paintEvent(QPaintEvent *e);
};
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

SubWidget.cpp

#include "SubWidget.h"
SubWidget::SubWidget(QWidget *parent) : QWidget(parent)
{
    setStyleSheet("QWidget{background-color:  rgb(116, 220, 255);}");
}
SubWidget::~SubWidget(){}
void SubWidget::paintEvent(QPaintEvent *e)
{
    QPainter paint(this);
    QStyleOption option;
    option.init(this);
    style()->drawPrimitive(QStyle::PE_Widget, &option, &paint, this);
    QWidget::paintEvent(e);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

MainWidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include "SubWidget.h"
class MainWidget : public QWidget
{
    Q_OBJECT
private:
    QPushButton* btnFadeIn;
    QPushButton* btnFadeOut;
    QPushButton* btnZoonIn;
    QPushButton* btnZoonOut;
    QPushButton* btnMoveIn;
    QPushButton* btnMoveOut;
    QHBoxLayout* btnLayout;
    QVBoxLayout* mainLayout;
    SubWidget* subWidget;
public:
    MainWidget(QWidget *parent = nullptr);
    ~MainWidget();
};
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

MainWidget.cpp

#include "MainWidget.h"
constexpr int widgetWidth=600;
constexpr int widgetHeight=400;
MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
    setFixedSize(widgetWidth,widgetHeight);
    btnFadeIn  =new QPushButton(this);
    btnFadeOut  =new QPushButton(this);
    btnZoonIn  =new QPushButton(this);
    btnZoonOut  =new QPushButton(this);
    btnMoveIn  =new QPushButton(this);
    btnMoveOut  =new QPushButton(this);
    btnLayout=new QHBoxLayout();
    mainLayout=new QVBoxLayout();
    subWidget=new SubWidget(this);
    subWidget->hide();
    btnFadeIn->setText(tr("FadeIn"));
    btnFadeOut->setText(tr("FadeOut"));
    btnZoonIn->setText(tr("ZoonIn"));
    btnZoonOut->setText(tr("ZoonOut"));
    btnMoveIn->setText(tr("MoveIn"));
    btnMoveOut->setText(tr("MoveOut"));
    btnLayout->setMargin(0);
    btnLayout->setSpacing(9);
    btnLayout->addWidget(btnFadeIn);
    btnLayout->addWidget(btnFadeOut);
    btnLayout->addWidget(btnZoonIn);
    btnLayout->addWidget(btnZoonOut);
    btnLayout->addWidget(btnMoveIn);
    btnLayout->addWidget(btnMoveOut);
    mainLayout->setMargin(9);
    mainLayout->addLayout(btnLayout);
    mainLayout->addStretch(1);
    setLayout(mainLayout);
    //淡入
    connect(btnFadeIn,&QPushButton::clicked,this,[=](bool){
        subWidget->setFixedSize(widgetWidth-20,widgetHeight-50);
        subWidget->setGeometry(10,40,subWidget->width(),subWidget->height());
        QPropertyAnimation* m_pAnimation = new QPropertyAnimation();
        m_pAnimation->setTargetObject(subWidget);
        m_pAnimation->setDuration(2000);
        QGraphicsOpacityEffect* m_pOpacity = new QGraphicsOpacityEffect();
        subWidget-> setGraphicsEffect(m_pOpacity);
        m_pOpacity->setOpacity(1);
        m_pAnimation->setTargetObject(m_pOpacity);
        m_pAnimation->setPropertyName("opacity");
        m_pAnimation->setStartValue(0);
        m_pAnimation->setEndValue(1);
        m_pAnimation->start();
        subWidget->show();
        connect(m_pAnimation,&QPropertyAnimation::finished,this,[subWidget=subWidget,m_pAnimation,m_pOpacity]{
            if(m_pAnimation!=Q_NULLPTR){
                delete m_pAnimation;
            }
            if(m_pOpacity!=Q_NULLPTR){
                delete m_pOpacity;
            }
            if(subWidget!=Q_NULLPTR){
                subWidget->hide();
            }
        });
    });    
	//淡出
    connect(btnFadeOut,&QPushButton::clicked,this,[=](bool){
        subWidget->setFixedSize(widgetWidth-20,widgetHeight-50);
        subWidget->setGeometry(10,40,subWidget->width(),subWidget->height());
        QPropertyAnimation* m_pAnimation = new QPropertyAnimation();
        m_pAnimation->setTargetObject(subWidget);
        m_pAnimation->setDuration(2000);
        QGraphicsOpacityEffect* m_pOpacity = new QGraphicsOpacityEffect();
        subWidget-> setGraphicsEffect(m_pOpacity);
        m_pOpacity->setOpacity(1);
        m_pAnimation->setTargetObject(m_pOpacity);
        m_pAnimation->setPropertyName("opacity");
        m_pAnimation->setStartValue(1);
        m_pAnimation->setEndValue(0);
        m_pAnimation->start();
        subWidget->show();
        connect(m_pAnimation,&QPropertyAnimation::finished,this,[subWidget=subWidget,m_pAnimation,m_pOpacity]{
            if(m_pAnimation!=Q_NULLPTR){
                delete m_pAnimation;
            }
            if(m_pOpacity!=Q_NULLPTR){
                delete m_pOpacity;
            }
            if(subWidget!=Q_NULLPTR){
                subWidget->hide();
            }
        });
    });
    //变大
    connect(btnZoonIn,&QPushButton::clicked,this,[=](bool){
        QPropertyAnimation* m_pAnimation = new QPropertyAnimation();
        m_pAnimation->setTargetObject(subWidget);
        m_pAnimation->setPropertyName("geometry");
        m_pAnimation->setDuration(2000);
        QRect stopRect  = QRect(10,40,widgetWidth-20,widgetHeight-50);
        QRect startRect = QRect(stopRect.center(), QSize(0, 0));
        m_pAnimation->setStartValue(startRect);
        m_pAnimation->setEndValue(stopRect);
        m_pAnimation->start();
        subWidget->show();
        connect(m_pAnimation,&QPropertyAnimation::finished,this,[subWidget=subWidget,m_pAnimation]{
            if(m_pAnimation!=Q_NULLPTR){
                delete m_pAnimation;
            }
            if(subWidget!=Q_NULLPTR){
                subWidget->hide();
            }
        });
    });
    
	//变小
    connect(btnZoonOut,&QPushButton::clicked,this,[=](bool){
        QPropertyAnimation* m_pAnimation = new QPropertyAnimation();
        m_pAnimation->setTargetObject(subWidget);
        m_pAnimation->setPropertyName("geometry");
        m_pAnimation->setDuration(2000);
        QRect startRect = QRect(10,40,widgetWidth-20,widgetHeight-50);
        QRect stopRect = QRect(startRect.center(), QSize(0, 0));
        m_pAnimation->setStartValue(startRect);
        m_pAnimation->setEndValue(stopRect);
        m_pAnimation->start();
        subWidget->show();
        connect(m_pAnimation,&QPropertyAnimation::finished,this,[subWidget=subWidget,m_pAnimation]{
            if(m_pAnimation!=Q_NULLPTR){
                delete m_pAnimation;
            }
            if(subWidget!=Q_NULLPTR){
                subWidget->hide();
            }
        });
    });
    //移入
    connect(btnMoveIn,&QPushButton::clicked,this,[=](bool){
        subWidget->setFixedSize(widgetWidth-20,widgetHeight-50);
        subWidget->setGeometry(10,40,subWidget->width(),subWidget->height());
        QPropertyAnimation* m_pAnimation = new QPropertyAnimation();
        m_pAnimation->setTargetObject(subWidget);
        m_pAnimation->setPropertyName("pos");
        m_pAnimation->setDuration(2000);
        m_pAnimation->setEndValue (QPoint(0,40));
        m_pAnimation->setStartValue(QPoint(width(), 40));
        m_pAnimation->start();
        subWidget->show();
        connect(m_pAnimation,&QPropertyAnimation::finished,this,[subWidget=subWidget,m_pAnimation]{
            if(m_pAnimation!=Q_NULLPTR){
                delete m_pAnimation;
            }
            if(subWidget!=Q_NULLPTR){
                subWidget->hide();
            }
        });
    });
    
	//移出
    connect(btnMoveOut,&QPushButton::clicked,this,[=](bool){
        subWidget->setFixedSize(widgetWidth-20,widgetHeight-50);
        subWidget->setGeometry(10,40,subWidget->width(),subWidget->height());
        QPropertyAnimation* m_pAnimation = new QPropertyAnimation();
        m_pAnimation->setTargetObject(subWidget);
        m_pAnimation->setPropertyName("pos");
        m_pAnimation->setDuration(2000);
        m_pAnimation->setStartValue(QPoint(0,40));
        m_pAnimation->setEndValue(QPoint(width(), 40));
        m_pAnimation->start();
        subWidget->show();
        connect(m_pAnimation,&QPropertyAnimation::finished,this,[subWidget=subWidget,m_pAnimation]{
            if(m_pAnimation!=Q_NULLPTR){
                delete m_pAnimation;
            }
            if(subWidget!=Q_NULLPTR){
                subWidget->hide();
            }
        });
    });
}
MainWidget::~MainWidget()
{
    if(btnLayout!=Q_NULLPTR){
        delete btnLayout;
        btnLayout=Q_NULLPTR;
    }
    if(mainLayout!=Q_NULLPTR){
        delete  mainLayout;
        mainLayout=Q_NULLPTR;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188

main.cpp

#include "MainWidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget mainWidget;
    mainWidget.show();
    return a.exec();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/292673
推荐阅读
相关标签
  

闽ICP备14008679号