Qt之二維繪圖:設置動畫效果

#include <QGraphicsLineItem>
#include <QGraphicsTextItem>
#include <QGraphicsPixmapItem>
#include <QGraphicsItemAnimation>  //動畫
#include <QTimeLine>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QGraphicsLineItem *lineItem;
    QGraphicsTextItem *textItem;  //由於要設置屬性,因此把它單獨提出來
    QGraphicsPixmapItem *pixmapItem;
     _view = new QGraphicsView(this);
     _view->setBackgroundBrush(QBrush(Qt::gray));  //設置_view的背景色
     _view->setScene(_scene = new QGraphicsScene);
     _scene->addItem(lineItem = new QGraphicsLineItem(QLineF(0, 0, 100, 100)));
     _scene->addItem(textItem = new QGraphicsTextItem("hello world"));
     textItem->setPos(QPointF(100, 100));
     textItem->setFont(QFont("aaa", 50, 100, true));  //設置字體
     
     
     
     _scene->addItem(pixmapItem = new QGraphicsPixmapItem(QPixmap("../test1.bmp")));
     pixmapItem->setScale(0.2);
     pixmapItem->setFlag(QGraphicsItem::ItemIsMovable);

     QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
     animation->setItem(pixmapItem);

     QTimeLine *timeLine = new QTimeLine(3000);  //3ms動一次
     timeLine->setLoopCount(100);   //設置循環次數
     
     animation->setTimeLine(timeLine);  //怎麼動
     animation->setTranslationAt(1, 200, 200);  //step, dx, dy

     timeLine->start();   //開始循環
}

void Widget::resizeEvent(QResizeEvent *)
{
    _view->setGeometry(QRect(QPoint(0, 0), size())); //設置_view的大小爲widget的大小
}
相關文章
相關標籤/搜索