Qt状态机场景模拟-续

来源:互联网 发布:达内java速成班 编辑:程序博客网 时间:2024/06/07 08:28

Qt状态机场景模拟-续

    • Qt状态机场景模拟-续
      • 简述
      • 效果图
      • 代码
      • 结尾

简述

在上篇中Qt状态机场景模拟,应用了状态机和动画消息,做了简单小车过交通灯的模拟效果。这里,我又稍稍做了些效果,小车在前进过程中,轮子转动效果。

效果图

这里因录制原因,效果不是很好,我做了三个状态截图
这里写图片描述

1.状态一
这里写图片描述

2.状态二
这里写图片描述

3.状态三
这里写图片描述

代码

//轮子元素绘制QRectF WheelItem::boundingRect() const{     return QRect(0, 0, 30, 30); }void WheelItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){        painter->setRenderHint(QPainter::Antialiasing);        painter->setPen(QPen(Qt::black, 1, Qt::SolidLine));//设置画笔形式         painter->setBrush(QBrush(QColor(209, 158, 85), Qt::SolidPattern));//设置画刷形式         //绘制轮子        painter->drawEllipse(0, 0, 30, 30);        //绘制轮子中间的线        painter->drawLine(1, 15, 29, 15);}WheelWidget::WheelWidget(QWidget *parent)    : QWidget(parent){    setStyleSheet("background: transparent");    QGraphicsScene* scene = new QGraphicsScene(this);    WheelItem* wheelItem = new WheelItem;    scene->addItem(wheelItem);    view = new QGraphicsView(this);    view->setStyleSheet("border:none; background:transparent;");    view->setRenderHint(QPainter::Antialiasing);    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    view->setScene(scene);    setFixedSize(60, 60);    setProperty("rotation", 0);}void WheelWidget::setRotation(int rotation){    //设置轮子旋转角度    auto itemLst = view->items();    for (auto itor : itemLst)    {        itor->setTransformOriginPoint(itor->boundingRect().width() / 2, itor->boundingRect().height() / 2);        itor->setRotation(-1 * rotation);    }}
//轮子旋转动画//并行动画,也有相应的串行动画QSequentialAnimationGroup        QParallelAnimationGroup* group = new QParallelAnimationGroup;//车子行走        QPropertyAnimation *animation = new QPropertyAnimation(m_carWidget, "pos");        animation->setDuration(2500);        animation->setEndValue(QPoint(200, 300));//左轮旋转        QPropertyAnimation *Lanimation = new QPropertyAnimation(m_carWidget->LwheelWidget, "rotation");        Lanimation->setDuration(2500);        Lanimation->setStartValue(0);        Lanimation->setEndValue(360);//右轮旋转        QPropertyAnimation *Ranimation = new QPropertyAnimation(m_carWidget->RwheelWidget, "rotation");        Ranimation->setDuration(2500);        Ranimation->setStartValue(0);        Ranimation->setEndValue(360);//开始动画        group->addAnimation(animation);        group->addAnimation(Lanimation);        group->addAnimation(Ranimation);        group->start(QAbstractAnimation::DeleteWhenStopped);

结尾

只为记录,只为分享! 愿所写能对你有所帮助。
主要代码都在上面,工程就不上传了,需要工程文件的,留言,或者加QQ。我发给各位。

原创粉丝点击