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。我发给各位。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨田哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值