#ifndefANIMATIONWIDGET_H#define ANIMATIONWIDGET_H#include<QMainWindow>#include<QWidget>#include<QPushButton>#include<QDesktopWidget>// 动画窗口classAnimationWidget:publicQWidget{Q_OBJECTpublic:explicitAnimationWidget(QWidget*parent=nullptr);publicslots:voidopacity();voidmax();voidmin();voidleft();voidright();voidup();voiddown();private:QDesktopWidget*m_pDesktopWidget;intm_nX;intm_nY;QPushButton*m_pOpacityBtn;//透明QPushButton*m_pMaxBtn;//放大QPushButton*m_pMinBtn;//缩小QPushButton*m_pLeftBtn;//左移QPushButton*m_pRightBtn;//右移QPushButton*m_pUpBtn;//上移QPushButton*m_pDownBtn;//下移};#endif// ANIMATIONWIDGET_H
#include"AnimationWidget.h"#include"MyType.h"#include<QGridLayout>#include<QPropertyAnimation>#include<QDesktopWidget>#include<QApplication>AnimationWidget::AnimationWidget(QWidget*parent):QWidget(parent){this->setWindowTitle(ZH_CN("动画窗口"));this->resize(400,150);this->setAttribute(Qt::WA_DeleteOnClose);m_pOpacityBtn=newQPushButton(ZH_CN("透明后还原"),this);connect(m_pOpacityBtn,SIGNAL(clicked(bool)),this,SLOT(opacity()));m_pMaxBtn=newQPushButton(ZH_CN("放大后还原"),this);connect(m_pMaxBtn,SIGNAL(clicked(bool)),this,SLOT(max()));m_pMinBtn=newQPushButton(ZH_CN("缩小后还原"),this);connect(m_pMinBtn,SIGNAL(clicked(bool)),this,SLOT(min()));m_pLeftBtn=newQPushButton(ZH_CN("左移后还原"),this);connect(m_pLeftBtn,SIGNAL(clicked(bool)),this,SLOT(left()));m_pRightBtn=newQPushButton(ZH_CN("右移后还原"),this);connect(m_pRightBtn,SIGNAL(clicked(bool)),this,SLOT(right()));m_pUpBtn=newQPushButton(ZH_CN("上移后还原"),this);connect(m_pUpBtn,SIGNAL(clicked(bool)),this,SLOT(up()));m_pDownBtn=newQPushButton(ZH_CN("下移后还原"),this);connect(m_pDownBtn,SIGNAL(clicked(bool)),this,SLOT(down()));QGridLayout*pLayout=newQGridLayout(this);pLayout->addWidget(m_pOpacityBtn,0,0,1,1);pLayout->addWidget(m_pMaxBtn,0,1,1,1);pLayout->addWidget(m_pMinBtn,1,0,1,1);pLayout->addWidget(m_pLeftBtn,1,1,1,1);pLayout->addWidget(m_pRightBtn,2,0,1,1);pLayout->addWidget(m_pUpBtn,2,1,1,1);pLayout->addWidget(m_pDownBtn,3,0,1,1);//获取窗口中心m_pDesktopWidget=QApplication::desktop();m_nX=(m_pDesktopWidget->width()-width())/2;m_nY=(m_pDesktopWidget->height()-height())/2;// //窗口左上角的位置(含边框)// qDebug() << this->frameGeometry().x() << this->frameGeometry().y() << ;//1// qDebug() << this->x() << this->y();//2// qDebug() << this->pos().x() << this->pos().y();//3// //窗口的宽度和高度(含边框)// qDebug() << this->frameGeometry().width() << this->frameGeometry().height();// //窗口左上角的位置(不含边框)// qDebug() << this->geometry().x() << this->geometry().y();// //窗口的宽度和高度(不含边框)// qDebug() << this->geometry().width() << this->geometry().height();//1// qDebug() << this->width() << this->height();//2// qDebug() << this->rect().width() << this->rect().height();//3// qDebug() << this->size().width() << this->size().height();//4}voidAnimationWidget::opacity(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"windowOpacity");pAnimation->setDuration(2000);pAnimation->setKeyValueAt(0,1);pAnimation->setKeyValueAt(0.5,0);pAnimation->setKeyValueAt(1,1);pAnimation->start(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::max(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"geometry");QRect startRect=QRect(QPoint(this->geometry().x(),this->geometry().y()),QSize(width(),height()));QRect stopRect=QRect(QPoint(0,0),QSize(m_pDesktopWidget->width(),m_pDesktopWidget->height()));pAnimation->setDuration(2000);pAnimation->setKeyValueAt(0,startRect);pAnimation->setKeyValueAt(0.5,stopRect);pAnimation->setKeyValueAt(1,startRect);pAnimation>start(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::min(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"geometry");QRect startRect=QRect(QPoint(this->geometry().x(),this->geometry().y()),QSize(width(),height()));QRect stopRect=QRect(startRect.center(),QSize(0,0));pAnimation->setDuration(2000);// pAnimation->setStartValue(startRect);// pAnimation->setEndValue(stopRect);pAnimation->setKeyValueAt(0,startRect);pAnimation->setKeyValueAt(0.5,stopRect);pAnimation->setKeyValueAt(1,startRect);pAnimation->start(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::left(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"pos");pAnimation->setDuration(2000);//控制在桌面内 /*pAnimation->setKeyValueAt(0, QPoint(m_nX, m_nY)); pAnimation->setKeyValueAt(0.5, QPoint(m_nX - width(), m_nY)); pAnimation->setKeyValueAt(1, QPoint(m_nX, m_nY));*/ //移动到桌面外 pAnimation->setKeyValueAt(0, QPoint(m_nX, m_nY)); pAnimation->setKeyValueAt(0.5, QPoint(0 - width() - 50, m_nY)); pAnimation->setKeyValueAt(1, QPoint(m_nX, m_nY)); pAnimation->start(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::right(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"pos");pAnimation->setDuration(2000);//移动到桌面外pAnimation->setKeyValueAt(0,QPoint(m_nX,m_nY));pAnimation->setKeyValueAt(0.5,QPoint(m_pDesktopWidget->width()+50,m_nY));pAnimation->setKeyValueAt(1,QPoint(m_nX,m_nY));pAnimation->start(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::up(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"pos");pAnimation->setDuration(2000);//移动到桌面外pAnimation->setKeyValueAt(0,QPoint(m_nX,m_nY));pAnimation->setKeyValueAt(0.5,QPoint(m_nX,0-height()-50));pAnimation->setKeyValueAt(1,QPoint(m_nX,m_nY));pAnimation->start(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::down(){QPropertyAnimation*pAnimation=newQPropertyAnimation(this,"pos");pAnimation->setDuration(2000);//移动到桌面外 pAnimation->setKeyValueAt(0, QPoint(m_nX, m_nY)); pAnimation->setKeyValueAt(0.5, QPoint(m_nX, m_pDesktopWidget->height() + 50)); pAnimation->setKeyValueAt(1, QPoint(m_nX, m_nY)); pAnimation->start(QAbstractAnimation::DeleteWhenStopped);}
AnimationWidget*pWidget=newAnimationWidget;pWidget->show();