Important: Please read the Qt Code of Conduct -

Draggable Bezier curve points with QPainterPath in QGraphicsView

  • I want to draw a Bezier curve on a QGraphicsView, and have the 2 middle control points be draggable. Here's where I'm at now:

    -I have a class called CurvesGraphicsView that derives from QGraphicsView.
    -I can create a QGraphicsEllipseItem by calling addEllipse() in the CurvesGraphicsView constructor.
    -This ellipse is be draggable without any extra code on my part (nice!).

    My problem is, how do I get this draggable point to be assigned to the curve point, and have the curve draw properly?

    From what I see, this is impossible because a curve can only be drawn once per object construction. Example:

    To draw, you must do this every time in mouseMoveEvent():

    QRectF rect = scene()->sceneRect();
    QPainterPath path(rect.bottomLeft());//Must construct a new one every time, they are not reusable.

    m_MousePos = mapToScene(e->x(), e->y());
    path.cubicTo(m_MousePos, m_MousePos, rect.topRight());
    scene()->clear();//Removes the previous curve, but also removes the ellipse, which is not what we want to do.

    -It cannot be kept as a member and drawn with new points a second time.
    -This requires clearing of the scene each time you want to draw a curve.
    -Clearing the scene removes the original ellipse item, defeating the purpose.

    Can anyone provide an example that shows how to draw two ellipses on the control points of the Bezier curve, and update that curve in response to a drag event on either of the points? I have a very simple need, yet despite pages and pages of documentation, cannot figure out how to implement it.


  • Maybe this is kind of what you want.

Log in to reply