33 Qt 之绘图之绘制卡通蚂蚁

绘制

具体的效果如下所示,我们可以再进行更好的完善。

源码

主要分为以下三部:

  • 绘制屁股
  • 绘制肚子
  • 绘制头部

注意:绘制的时候,由于各个部分的颜色不同,而且坐标不好定位,所以我们采用的图形覆盖的方式。

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter :: Antialiasing, true);

    /*****屁股*****/
    QPainterPath path;
    path.addRoundRect(QRect(200, 60, 150, 150), 1000);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::black);
    painter.drawPath(path);

    /*****肚子*****/
    // 腿
    path = QPainterPath();
    path.moveTo(170, 180);
    path.lineTo(120, 260);
    path.moveTo(185, 180);
    path.lineTo(145, 280);
    path.moveTo(200, 180);
    path.lineTo(180, 290);

    path.moveTo(200, 180);
    path.lineTo(220, 290);
    path.moveTo(215, 180);
    path.lineTo(250, 280);
    path.moveTo(230, 180);
    path.lineTo(280, 260);
    painter.setBrush(Qt::NoBrush);
    painter.setPen(Qt::white);
    painter.drawPath(path);

    // 肚子
    path = QPainterPath();
    path.addRoundRect(QRect(150, 130, 100, 100), 1000);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::black);
    painter.drawPath(path);

    /*****头*****/
    // 犄角
    path = QPainterPath();
    path.moveTo(80, 100);
    path.lineTo(60, 20);
    path.moveTo(140, 100);
    path.lineTo(160, 20);
    painter.setBrush(Qt::NoBrush);
    painter.setPen(Qt::white);
    painter.drawPath(path);

    path = QPainterPath();
    path.addRoundRect(QRect(50, 80, 120, 120), 1000);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::black);
    painter.drawPath(path);

    // 左眼
    path = QPainterPath();
    path.addRoundRect(QRect(70, 120, 25, 25), 1000);
    painter.setBrush(Qt::black);
    painter.setPen(Qt::NoPen);
    painter.drawPath(path);

    path = QPainterPath();
    path.addRoundRect(QRect(75, 126, 10, 10), 1000);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::NoPen);
    painter.drawPath(path);

    // 右眼
    path = QPainterPath();
    path.addRoundRect(QRect(120, 110, 25, 25), 1000);
    painter.setBrush(Qt::black);
    painter.setPen(Qt::NoPen);
    painter.drawPath(path);

    path = QPainterPath();
    path.addRoundRect(QRect(125, 118, 10, 10), 1000);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::NoPen);
    painter.drawPath(path);

    // 嘴
    path = QPainterPath();
    path.moveTo(160, 108);
    path.arcTo(QRect(130, 48, 60, 60), 270, 100);
    painter.rotate(30);
    painter.setBrush(Qt::NoBrush);
    painter.setPen(Qt::black);
    painter.drawPath(path);
}

猜你喜欢

转载自blog.csdn.net/Chiang2018/article/details/102762796
33