一个QT 3D转动控件

其实说到底就是不停的截图,做出的幻觉。联想起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 = 0);
        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=0;
    isAnimating=false;
}
void RotatingStackedWidget::paintEvent(QPaintEvent * event)
{
    if(isAnimating)
    {
        if(iRotateVal > 90)
        {
            QPixmap pixmap(widget(nextIndex)->size());
            widget(nextIndex)->render(&pixmap);
            QPainter painter(this);

            QTransform transform;
            transform.translate(width()/2, 0);
            transform.rotate(iRotateVal+180,Qt::YAxis);
            painter.setTransform(transform);
            painter.drawPixmap(-1*width()/2,0,pixmap);
        }
        else
        {
            QPixmap pixmap(currentWidget()->size());
            currentWidget()->render(&pixmap);
            QPainter painter(this);

            QTransform transform;
                 transform.translate(width()/2, 0);
                 transform.rotate(iRotateVal,Qt::YAxis);
                 painter.setTransform(transform);
            painter.drawPixmap(-1*width()/2,0,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 ( 0,  0, offsetx, offsety );

    QPropertyAnimation *animnow = new QPropertyAnimation(this,"rotateVal");

    animnow->setDuration(500);
    animnow->setEasingCurve(QEasingCurve::Linear);
    animnow->setStartValue(0);
    animnow->setEndValue(180);
    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=0;
    isAnimating=false;
    widget(nextIndex)->show();
    widget(nextIndex)->raise();;
    setCurrentWidget(widget(nextIndex));
    repaint();

}

参考:https://github.com/jun-zhang/Qt-Rotating-Widget

posted @ 2015-08-07 17:15  findumars  Views(2143)  Comments(0Edit  收藏  举报