QGraphicsItem画五角星 C++
时间: 2024-01-24 19:08:18 浏览: 27
以下是一个画五角星的QGraphicsItem示例代码:
```cpp
#include <QPainter>
#include <QPointF>
#include <QVector>
class StarItem : public QGraphicsItem
{
public:
StarItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent) {}
QRectF boundingRect() const override
{
return QRectF(-50, -50, 100, 100);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
Q_UNUSED(option);
Q_UNUSED(widget);
QVector<QPointF> points;
const double pi = 3.14159265358979323846;
const double deg = pi / 180.0;
const double angle = 36.0 * deg;
const double radius = 50.0;
for (int i = 0; i < 5; ++i)
{
const double x = radius * qCos(angle * i);
const double y = radius * qSin(angle * i);
points << QPointF(x, y);
}
painter->drawPolygon(QPolygonF(points));
}
};
```
在场景中添加该项:
```cpp
StarItem *starItem = new StarItem();
starItem->setPos(100, 100);
scene->addItem(starItem);
```
注意,该示例代码使用的是QGraphicsItem的paint()函数进行绘制。如果您使用的是QGraphicsObject,可以在其子类中重写paintEvent()函数进行绘制。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)