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
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);
}
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
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;
}
}
main.cpp
#include "MainWidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget mainWidget;
mainWidget.show();
return a.exec();
}