qgraphicsview中的免费手绘

时间:2013-11-22 05:28:44

标签: qt qgraphicsview

我已经完成了如何绘制自由形式的涂鸦示例。我希望在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);

1 个答案:

答案 0 :(得分:2)

我做到了 使用

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);
} 
相关问题