码迷,mamicode.com
首页 > 其他好文 > 详细

一个QT 3D转动控件

时间:2015-08-07 19:10:53      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

其实说到底就是不停的截图,做出的幻觉。联想起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

一个QT 3D转动控件

标签:

原文地址:http://www.cnblogs.com/findumars/p/4711391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!