QWidget的窗口动画,Qt窗口各种动画效果合集,包括透明度、放大、缩小、上下左右平移等。
#ifndefANIMATIONWIDGET_H#define ANIMATIONWIDGET_H#includeQMainWindow#includeQWidget#includeQPushButton#includeQDesktopWidget// 动画窗口classAnimationWidget:publicQWidget{Q_OBJECTpublic:explicitAnimationWidget(QWidget*parentnullptr);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#includeAnimationWidget.h#includeMyType.h#includeQGridLayout#includeQPropertyAnimation#includeQDesktopWidget#includeQApplicationAnimationWidget::AnimationWidget(QWidget*parent):QWidget(parent){this-setWindowTitle(ZH_CN(动画窗口));this-resize(400,150);this-setAttribute(Qt::WA_DeleteOnClose);m_pOpacityBtnnewQPushButton(ZH_CN(透明后还原),this);connect(m_pOpacityBtn,SIGNAL(clicked(bool)),this,SLOT(opacity()));m_pMaxBtnnewQPushButton(ZH_CN(放大后还原),this);connect(m_pMaxBtn,SIGNAL(clicked(bool)),this,SLOT(max()));m_pMinBtnnewQPushButton(ZH_CN(缩小后还原),this);connect(m_pMinBtn,SIGNAL(clicked(bool)),this,SLOT(min()));m_pLeftBtnnewQPushButton(ZH_CN(左移后还原),this);connect(m_pLeftBtn,SIGNAL(clicked(bool)),this,SLOT(left()));m_pRightBtnnewQPushButton(ZH_CN(右移后还原),this);connect(m_pRightBtn,SIGNAL(clicked(bool)),this,SLOT(right()));m_pUpBtnnewQPushButton(ZH_CN(上移后还原),this);connect(m_pUpBtn,SIGNAL(clicked(bool)),this,SLOT(up()));m_pDownBtnnewQPushButton(ZH_CN(下移后还原),this);connect(m_pDownBtn,SIGNAL(clicked(bool)),this,SLOT(down()));QGridLayout*pLayoutnewQGridLayout(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_pDesktopWidgetQApplication::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*pAnimationnewQPropertyAnimation(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*pAnimationnewQPropertyAnimation(this,geometry);QRect startRectQRect(QPoint(this-geometry().x(),this-geometry().y()),QSize(width(),height()));QRect stopRectQRect(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);pAnimationstart(QAbstractAnimation::DeleteWhenStopped);}voidAnimationWidget::min(){QPropertyAnimation*pAnimationnewQPropertyAnimation(this,geometry);QRect startRectQRect(QPoint(this-geometry().x(),this-geometry().y()),QSize(width(),height()));QRect stopRectQRect(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*pAnimationnewQPropertyAnimation(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*pAnimationnewQPropertyAnimation(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*pAnimationnewQPropertyAnimation(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*pAnimationnewQPropertyAnimation(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*pWidgetnewAnimationWidget;pWidget-show();