QT 图片旋转--QTransform 沿中间旋转

本文介绍了一个使用Qt实现带有阴影效果的旋转动画的示例。通过创建自定义QWidget并利用QGraphicsDropShadowEffect和QTimeLine,实现了图片的动态旋转效果。文中详细展示了如何设置场景、添加图片项及动画控制。
#ifndef shadow_H_
#define shadow_H_
#include <QtGui/QtGui>
#include <QtCore/QtCore>

class ShadowWidget : public QWidget
{
    Q_OBJECT
public:
    ShadowWidget(QWidget *parent =0)
    {
        //setWindowFlags(Qt::FramelessWindowHint);
        QGraphicsDropShadowEffect *fEffect =  new QGraphicsDropShadowEffect;
        //fEffect->setColor(QColor(255,0,0));
        QVBoxLayout *layout = new QVBoxLayout(this);
        QGraphicsScene *scene = new QGraphicsScene;
        scene->setSceneRect(QRect(0,0,900,600));
         fPixmap= new QGraphicsPixmapItem;
        fPixmap->setPos(200,200);
        QPixmap *pix = new QPixmap("1.jpg");
        fPixmap->setPixmap(*pix);
        //fPixmap->setGraphicsEffect(fEffect);
        QTransform *tran = new QTransform;
        tran->rotate(80,Qt::YAxis);
        //fPixmap->setTransform(*tran);
        //fPixmap->setTransformOriginPoint(QPointF(pix->size().width()/2,pix->size().height()/2));
        //fPixmap->setTransformOriginPoint(100,100);
        scene->addItem(fPixmap);
        QGraphicsView *view = new QGraphicsView(scene);
        layout->addWidget(view);
        QPushButton * animButton = new QPushButton(tr("Start"));
        layout->addWidget(animButton);
        connect(animButton,SIGNAL(clicked()),this,SLOT(performAnimation()));
        //setGraphicsEffect(fEffect);
        resize(900,600);
        fTimeLine = NULL;
    }

private slots:
    void performAnimation()
    {
        if(!fTimeLine)
        {
            fTimeLine = new QTimeLine;
            fTimeLine->setDuration(1000);
            fTimeLine->setFrameRange(1,10);
            connect(fTimeLine,SIGNAL(frameChanged(int)),this,SLOT(performRotateAnimation(int )));
        }

        fTimeLine->start();
    }

    void performRotateAnimation(int frame)
    {
        qreal angle = 90*(frame)/10.0;
        QTransform tran;
        tran.translate(150,0);
        //fPixmap->setTransformOriginPoint(0,0);
        tran.rotate(angle,Qt::YAxis);
        fPixmap->setTransform(tran);
        QTransform t;
        t.translate(-150,0);
        fPixmap->setTransform(t,true);
    }
private:
    QGraphicsPixmapItem *fPixmap;
    QTimeLine *fTimeLine ;
};
#endif
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值