1 填充多边形绘制形式
void GraphicsPolygonItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{Q_UNUSED(option);Q_UNUSED(widget);//painter->setPen(pen()); // 设置默认画笔//painter->setBrush(brush()); // 设置默认画刷// 创建一个线性渐变的笔刷,// 不仅会根据设定的画笔(pen())绘制多边形的轮廓,还会使用当前设置的笔刷(即线性渐变)来填充多边形内部区域。QColor c = brushColor();QLinearGradient result(boundingRect().topLeft(), boundingRect().topRight());result.setColorAt(0, c.dark(150));result.setColorAt(0.5, c.light(200));result.setColorAt(1, c.dark(150));painter->setBrush(result);// 设置画笔并绘制多边形painter->setPen(pen()); // 默认画笔参数,绘制多边形边缘painter->drawPolygon(m_points);// 如果项被选中,绘制选中高亮效果if (option->state & QStyle::State_Selected)qt_graphicsItem_highlightSelected(this, painter, option);
}
2 不填充
只通过画笔,绘制多边形轮廓
void GraphicsPolygonItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{Q_UNUSED(option);Q_UNUSED(widget);// 创建一个红色的画笔,并设置线宽为1QPen pen;pen.setColor(Qt::red);pen.setWidth(1);// 设置画笔并绘制多边形painter->setPen(pen);painter->setBrush(Qt::NoBrush); // 不填充painter->drawPolygon(m_points);// 如果项被选中,绘制选中高亮效果if (option->state & QStyle::State_Selected)qt_graphicsItem_highlightSelected(this, painter, option);
}