zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    Python 初识爬虫-**机场出港业务
    Python 基础学习之字典
    Python 基础学习之if语句
    初识 超级账本
    搭建element-ui Vue结构
    回归
    Gin框架body参数获取
    log4go折腾
    go获取当前执行的位置程序
    mybatis generator 整合lombok
  • 原文地址:https://www.cnblogs.com/cLockey/p/4238432.html
Copyright © 2011-2022 走看看