效果
代码
class drawRabbit: public QWidget {
public:
drawRabbit(QWidget *parent = nullptr) : QWidget(parent) {}
private:
void paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// 绘制兔子的耳朵
painter.setPen(Qt::NoPen);
painter.setBrush(QColor("#f2a272"));
painter.drawEllipse(QRectF(50, 30, 40, 80));
painter.drawEllipse(QRectF(140, 30, 40, 80));
painter.setBrush(QColor("#ffb7a2"));
painter.drawEllipse(QRectF(52, 38, 36, 66));
painter.drawEllipse(QRectF(142, 38, 36, 66));
// 绘制兔子的脸
painter.setBrush(QColor("#f7c2b7"));
painter.drawEllipse(QRectF(70, 70, 100, 130));
// 绘制兔子的眼睛
painter.setPen(QPen(Qt::white, 4));
painter.setBrush(QColor("#ffffff"));
painter.drawEllipse(QRectF(90, 100, 30, 40));
painter.drawEllipse(QRectF(130, 100, 30, 40));
painter.setPen(QPen(Qt::black, 2));
painter.setBrush(QColor("#000000"));
painter.drawEllipse(QRectF(97, 110, 16, 22));
painter.drawEllipse(QRectF(137, 110, 16, 22));
painter.setBrush(QColor("#ffffff"));
painter.drawEllipse(QRectF(103, 115, 5, 5));
painter.drawEllipse(QRectF(143, 115, 5, 5));
// 绘制兔子的嘴巴
painter.setPen(QPen(QColor("#d95259"), 4));
painter.setBrush(QColor("#d95259"));
painter.drawEllipse(QRectF(100, 145, 60, 40));
painter.setBrush(QColor("#ffffff"));
painter.drawEllipse(QRectF(110, 150, 10, 10));
painter.drawEllipse(QRectF(140, 150, 10, 10));
// 绘制兔子的身体
painter.setBrush(QColor("#f7c2b7"));
painter.drawEllipse(QRectF(20, 200, 200, 150));
// 绘制兔子的手臂
painter.setPen(Qt::NoPen);
painter.setBrush(QColor("#f2a272"));
painter.drawEllipse(QRectF(10, 210, 50, 80));
painter.drawEllipse(QRectF(210, 210, 50, 80));
painter.setBrush(QColor("#ffb7a2"));
painter.drawEllipse(QRectF(12, 218, 46, 66));
painter.drawEllipse(QRectF(212, 218, 46, 66));
// 绘制兔子的腿
painter.setPen(Qt::NoPen);
painter.setBrush(QColor("#f2a272"));
painter.drawEllipse(QRectF(60, 320, 50, 80));
painter.drawEllipse(QRectF(140, 320, 50, 80));
painter.setBrush(QColor("#ffb7a2"));
painter.drawEllipse(QRectF(62, 328, 46, 66));
painter.drawEllipse(QRectF(142, 328, 46, 66));
}
};
.main
QApplication app(argc, argv);
drawRabbit ra;
ra.resize(240, 400);
ra.show();
return app.exec();