• 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);
    }
  • 相关阅读:
    2015/12/26 十六、 八 、二 进制转十进制
    2015/12/25 ① 图灵测试 ② 安装jdk出现的问题 ③ 配置环境变量
    java如何产生随机数
    二分查找法
    冒泡排序法
    计算阶乘
    九九乘法小练习
    数组循环语句练习
    经典循环例题练习
    如何用循环语句输出一个三角形
  • 原文地址:https://www.cnblogs.com/cLockey/p/4238432.html
Copyright © 2020-2023  润新知