其实说到底就是不停的截图,做出的幻觉。联想起360拖动图片,也是合并图片做出的效果,可见的对GUI来说图片是一切,是最根本的解决一切问题的办法,编程仅是辅助实现手段而已,我要记住这一点。
.h文件
#ifndef WIDGET1_H
#define WIDGET1_H #include <QVariant>
#include <QWidget>
#include <QLabel>
#include <QStackedWidget>
class RotatingStackedWidget : public QStackedWidget
{
Q_OBJECT Q_PROPERTY( float rotateVal READ rotateVal WRITE setRotateVal);
public:
explicit RotatingStackedWidget(QWidget *parent = );
void paintEvent(QPaintEvent *);
void rotate(int); float rotateVal();
void setRotateVal(float); signals: private slots:
void valChanged(QVariant);
void animDone();
private:
float iRotateVal; bool isAnimating;
int nextIndex;
}; #endif // WIDGET1_H
.cpp文件:
#include "RotatingStackedWidget.h"
#include <QPixmap>
#include <QVBoxLayout>
#include <QPainter>
#include <QTransform>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QDebug> RotatingStackedWidget::RotatingStackedWidget(QWidget *parent) :
QStackedWidget(parent)
{
iRotateVal=;
isAnimating=false;
}
void RotatingStackedWidget::paintEvent(QPaintEvent * event)
{
if(isAnimating)
{
if(iRotateVal > )
{
QPixmap pixmap(widget(nextIndex)->size());
widget(nextIndex)->render(&pixmap);
QPainter painter(this); QTransform transform;
transform.translate(width()/, );
transform.rotate(iRotateVal+,Qt::YAxis);
painter.setTransform(transform);
painter.drawPixmap(-*width()/,,pixmap);
}
else
{
QPixmap pixmap(currentWidget()->size());
currentWidget()->render(&pixmap);
QPainter painter(this); QTransform transform;
transform.translate(width()/, );
transform.rotate(iRotateVal,Qt::YAxis);
painter.setTransform(transform);
painter.drawPixmap(-*width()/,,pixmap);
}
}
else
{
QWidget::paintEvent(event);
}
} void RotatingStackedWidget::rotate(int index)
{
if(isAnimating)
return; nextIndex = index; int offsetx=frameRect().width();
int offsety=frameRect().height(); widget(index)->setGeometry ( , , offsetx, offsety ); QPropertyAnimation *animnow = new QPropertyAnimation(this,"rotateVal"); animnow->setDuration();
animnow->setEasingCurve(QEasingCurve::Linear);
animnow->setStartValue();
animnow->setEndValue();
connect(animnow,SIGNAL(valueChanged(QVariant)),this,SLOT(valChanged(QVariant)));
connect(animnow,SIGNAL(finished()),this,SLOT(animDone())); currentWidget()->hide(); isAnimating = true;
animnow->start();;
} float RotatingStackedWidget::rotateVal()
{
return iRotateVal;
}
void RotatingStackedWidget::setRotateVal(float fl)
{
iRotateVal = fl;
} void RotatingStackedWidget::valChanged(QVariant)
{
repaint();
} void RotatingStackedWidget::animDone()
{
iRotateVal=;
isAnimating=false;
widget(nextIndex)->show();
widget(nextIndex)->raise();;
setCurrentWidget(widget(nextIndex));
repaint(); }
参考:https://github.com/jun-zhang/Qt-Rotating-Widget