用qpainter画一个背景为竹子的熊猫
时间: 2024-03-18 22:39:04 浏览: 123
好的,以下是用Qt中的QPainter类画一个背景为竹子的熊猫的代码:
```cpp
void Widget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
//画背景为竹子的矩形
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(QPixmap(":/images/bamboo.jpg")));
painter.drawRect(0, 0, width(), height());
//画身体
painter.setPen(QPen(Qt::black, 2));
painter.setBrush(Qt::white);
painter.drawEllipse(QPointF(150, 150), 100, 120);
//画耳朵
painter.setBrush(Qt::white);
painter.drawEllipse(QPointF(85, 80), 30, 40);
painter.drawEllipse(QPointF(215, 80), 30, 40);
//画眼睛
painter.setBrush(Qt::black);
painter.drawEllipse(QPointF(120, 130), 10, 20);
painter.drawEllipse(QPointF(180, 130), 10, 20);
//画鼻子
painter.setBrush(Qt::white);
painter.drawEllipse(QPointF(150, 160), 20, 20);
painter.setBrush(Qt::black);
painter.drawEllipse(QPointF(150, 170), 5, 5);
//画嘴巴
painter.setBrush(Qt::white);
painter.drawEllipse(QPointF(150, 190), 20, 10);
}
```
这段代码会在窗口中画出一个背景为竹子的熊猫。注意,这里使用了QPixmap来设置背景图片,需要先在项目文件中将图片添加进来。
阅读全文