qtqgraphicsview

free hand draw in qgraphicsview


i have gone through the scribble example for how to draw free form . i want the same free form draw using QGraphicsitem on qgraphicsview. i should draw it as a graphicsitem as i can move the selected free form every where in the scene. i tried this

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);

Solution

  • i did it using

    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);
    }