美文网首页
我的挖掘机

我的挖掘机

作者: Qt君 | 来源:发表于2019-12-11 22:43 被阅读0次

    非常感谢navca的分享。该例子基于QGraphicsItem绘图实现的挖掘机。(文末源码地址)

    demo.gif

    心中有坐标,万物皆可绘

      我的挖掘机例子共分为车体大臂小臂爪子四部分,全部皆为自绘。

    • 车体
    void Body::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setBrush(QBrush(Qt::darkYellow));
        painter->drawRect(0,0,220,100);
        painter->drawRect(100,-80,100,80);
    
        painter->setBrush(QBrush(Qt::darkYellow));
        painter->drawEllipse(_pinPoint1, 5,5);
        painter->drawEllipse(_pinPoint2, 5,5);
    
        painter->setPen(QPen(QBrush(Qt::gray), 6));
        QPointF p(mapFromItem(_bigHand, _bigHand->PinPoint));
        painter->drawLine(_pinPoint2.x(), _pinPoint2.y(), p.x(), p.y());
    
        //液压臂
        painter->setPen(QPen(QBrush(Qt::black), 10));
        QLineF line(_pinPoint2, p);
        qreal angle = line.angle();
        QPointF midPoint(_pinPoint2.x()+ 80*cos(qDegreesToRadians(angle)), _pinPoint2.y() - 80*sin(qDegreesToRadians(angle)));
        painter->drawLine(_pinPoint2, midPoint);
    }
    
    • 大臂
    void BigHand::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setPen(Qt::black);
        painter->setBrush(Qt::yellow);
    
        QPainterPath path;
        path.moveTo(-6,2);
        path.lineTo(60,-180);
        path.lineTo(250,-270);
        path.lineTo(258,-260);
        path.lineTo(90,-150);
        path.lineTo(5,5);
    
        path.moveTo(_pinPoint2.x()-10, _pinPoint2.y()+5);
        path.lineTo(_pinPoint2.x()-5, _pinPoint2.y()-8);
        path.lineTo(_pinPoint2.x()+8, _pinPoint2.y()-5);
        path.lineTo(_pinPoint2.x()-10, _pinPoint2.y()+5);
        painter->drawPath(path);
    
        painter->setBrush(Qt::darkYellow);
        painter->drawEllipse(-4,-5,10,10);
        painter->drawEllipse(PinPoint,5,5);
    
        painter->drawEllipse(_pinPoint1,5,5);
    
        painter->drawEllipse(_pinPoint2,5,5);
    
        painter->setPen(QPen(QBrush(Qt::gray), 6));
        QPointF p(mapFromItem(_smallHand, _smallHand->PinPoint));
        painter->drawLine(_pinPoint2.x(), _pinPoint2.y(), p.x(), p.y());
    
        //液压臂
        painter->setPen(QPen(QBrush(Qt::black), 10));
        QLineF line(_pinPoint2, p);
        qreal angle = line.angle();
        QPointF midPoint(_pinPoint2.x()+ 110*cos(qDegreesToRadians(angle)), _pinPoint2.y() - 110*sin(qDegreesToRadians(angle)));
        painter->drawLine(_pinPoint2, midPoint);
    }
    
    • 小臂
    void SmallHand::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setPen(Qt::black);
        painter->setBrush(Qt::yellow);
    
        QPainterPath path;
        path.moveTo(-50, -30);
        path.lineTo(200, 0);
        path.lineTo(198, 10);
        path.lineTo(-3, 5);
        path.lineTo(-55, -18);
        path.lineTo(-50, -30);
    
        path.moveTo(_pinPoint1.x()-10, _pinPoint1.y()+5);
        path.lineTo(_pinPoint1.x(), _pinPoint1.y()-6);
        path.lineTo(_pinPoint1.x()+10, _pinPoint1.y()+7);
        path.lineTo(_pinPoint1.x()-8, _pinPoint1.y()+5);
        painter->drawPath(path);
    
        painter->setBrush(Qt::darkYellow);
        painter->drawEllipse(-5,-5, 10, 10);
        painter->drawEllipse(PinPoint,5,5);
        painter->drawEllipse(_pinPoint1,5,5);
        painter->drawEllipse(_pinPoint2,5,5);
        painter->drawEllipse(_pinPoint3,5,5);
    
        painter->setPen(QPen(QBrush(Qt::gray), 6));
        QPointF p(mapFromItem(_littleHand, _littleHand->PinPoint));
        painter->drawLine(_pinPoint1.x(), _pinPoint1.y(), p.x(), p.y());
    
        painter->setPen(QPen(QBrush(Qt::black), 10));
        QLineF line(_pinPoint1, p);
        qreal angle = line.angle();
        QPointF midPoint(_pinPoint1.x()+ 90*cos(qDegreesToRadians(angle)), _pinPoint1.y() - 90*sin(qDegreesToRadians(angle)));
        painter->drawLine(_pinPoint1, midPoint);
    
        painter->setPen(QPen(QBrush(Qt::black), 10));
        QPointF p1(mapFromItem(_hand, _hand->PinPoint));
        painter->drawLine(p.x(), p.y(), p1.x(), p1.y());
    }
    
    • 爪子
    void Hand::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setPen(Qt::black);
        painter->setBrush(Qt::yellow);
    
        QPainterPath path;
        path.moveTo(-12, -30);
        path.lineTo(-3, -35);
        path.lineTo(-6, -44);
        path.lineTo(6, -48);
        path.lineTo(35, -45);
        path.lineTo(70, 0);
        path.lineTo(15, -10);
        path.lineTo(10, 0);
        path.lineTo(3, 7);
        path.lineTo(-3, 3);
        path.lineTo(-12, -30);
        painter->drawPath(path);
    
        painter->setBrush(Qt::darkYellow);
        painter->drawEllipse(PinPoint,5,5);
        painter->drawEllipse(_pinPoint1,5,5);
    }
    
    main_page.png

    源码地址(欢迎star)

    相关文章

      网友评论

          本文标题:我的挖掘机

          本文链接:https://www.haomeiwen.com/subject/apzagctx.html