Qt QGraphicsItem 绕中心旋转、放缩


最近用到了QGraphicsItem,可以通过QGraphicsItemAnimation使其产生动画效果。

QGraphicsItemAnimation自带了setPosAt()、setRotationAt()、setScaleAt()等方法可以用来移动、旋转、放缩QGraphicsItem,但其默认的OriginPoint是这个Item的左上角,虽然QGraphicsItem自带了setTransformOriginPoint()方法,但是设置以后没有效果,还是绕左上角放缩旋转,只好采取其他办法。从网上查了一番资料,最后用了下面这种矩阵变换的方法。

先设置QTimeLine:

    QTimeLine _timeLine;
        _timeLine.setDuration(3000);    //持续时间
    _timeLine.setLoopCount(0);        //无限循环
    _timeLine.setFrameRange(0, 100);//frameChanged()发出的值在0-100之间
    _timeLine.setCurveShape(QTimeLine::SineCurve);    //frameChanged()发出的值像sin曲线一样,1,2,...,99,100,99,...,2,1
    _timeLine.setUpdateInterval(25);    //更新频率(也就是frameChanged(int)的执行速度),每25ms更新一次,相当于每秒40帧,
    connect(&_timeLine, SIGNAL(frameChanged(int)), this, SLOT(scaleAnimation(int)));   
  _timeLine.start(); 

槽函数如下:

//头文件中的
private slots:
    void scaleAnimation(int frame); 

//源文件中的
void GraphicsItemAnimation::scaleAnimation(int frame)
{
    //_st是一个QGraphicsItem
    QRectF rect = _st->boundingRect();
    QPointF pt = _st->boundingRect().center();
    qreal scaleX_Y = (frame+50) / 100.0;
    QTransform tran;
    tran.translate(pt.x(), pt.y());
    tran.scale(scaleX_Y, scaleX_Y);
    _st->setTransform(tran);
    QTransform t;
    t.translate(-pt.x(), -pt.y());
    _st->setTransform(t, true);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM