33 Qt's drawing of drawing cartoon ants

draw

The specific effects are shown below, and we can make better improvements.

Source code

Mainly divided into the following three parts:

  • Draw ass
  • Draw belly
  • Draw head

Note: When drawing, because the colors of each part are different and the coordinates are not easy to locate, we adopt the method of graphic overlay.

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);
}

 

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/102762796