qgraphicsview中的手绘



我已经学习了如何绘制自由表单的scrible示例。我想要在qgraphicsview上使用QGraphicsitem进行相同的自由形式绘制。我应该把它画成一个图形项目,因为我可以在场景中的每个地方移动所选的自由形式。我试过这个

DrawnPathItem = this->scene()->addPath(QPainterPath());
QGraphicsLineItem liner;
liner.setLine( QLineF(startPoint, endPoint) );
liner.setPen(QPen(Qt::red));
QPainterPath path = DrawnPathItem->path();
path.setFillRule(Qt::WindingFill);
path.addPath( liner.shape() );
path = path.simplified();
DrawnPathItem->setPath(path);
我做到了使用
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
myPath = new QGraphicsPathItem();
previous = event->scenePos();
QPainterPath p;
p.moveTo(previous);
myPath->setPath(p);
this->addItem(myPath);
}
void ::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(myPath)
{
QPainterPath path = myPath->path();
previous = event->scenePos();
path.lineTo(previous);
myPath->setPath(path);
} 

最新更新