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