我有QGraphicsScene和我的物品。我的项继承自QGraphicsObject并覆盖paint方法。
创建项目
CLoco* newLoco = new CLoco();
if(Scene)
Scene->addItem(newLoco);
声明
class CLoco : public QGraphicsObject
{
QBrush oldBrush = painter->brush();
painter->setBrush(QBrush(color));
painter->drawEllipse(boundingRect());
painter->setBrush(oldBrush);
绘画方法
void CLoco::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
Q_UNUSED(widget);
QColor color = cColor;
if (isSelected())
color = cSelectedColor;
else
color = cColor;
const qreal detalizationLevel = option->levelOfDetailFromTransform(painter->worldTransform());
CurrentRadius = TemplateRadius / detalizationLevel + 0.5;
QBrush oldBrush = painter->brush();
painter->setBrush(QBrush(color));
painter->drawEllipse(boundingRect());
painter->setBrush(oldBrush);
}
我也使用默认的QGraphicsPathItem,我想在绘制这些对象时使用自定义笔。
QGraphicsPathItem* graphPath = new QGraphicsPathItem(*painterPath);
QPen pen(Qt::GlobalColor::cyan);
pen.setWidth(5);
graphPath->setPen(pen);
在此设置笔之后,所有其他项目均由此青色笔绘制。我是否可以在不创建新类的情况下仅为QGraphicsPathItem使用自定义笔,继承自QGraphicsPathItem并覆盖paint方法?