Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt - creating a curved path using N points



  • I'm developing a simulator gui in which the user clicks on different points of the map and the program connects these points to each other however the connection should be somehow curved (but preferably the curve should pass from the given points) I can't find a decent way to implement this.

    I have seen similar problems and often they are solved using QPainterPath or implementing a bezier curve. Or should I just compute the control points of the bezier curve (if so, how?) ?

    Any help would be appreciated,
    Thank you in advance



  • Are you using QML or Widgets for your GUI?



  • I'm using Widgets for my gui



  • QPainterPath cubicTo is indeed a good option..
    @
    QPainterPath path;
    path.moveTo(0, 0);

    path.cubicTo(99, 0, 50, 50, 99, 99);
    path.cubicTo(0, 99, 50, 50, 0, 0);
    @



  • With the Bézier curve segments the two control points control the tangent of the curve at the starting point and ending point (and do not sit on the curve). If you want to draw a smooth curve through a series of points then you can use cubicTo() for each consecutive pair of points with control points designed to maintain the same "arriving" and "departing" tangent at each mid point. That is, the second control point for segment n, the endpoint of segment n, and the first control point for segment n+1 should be co-linear.


Log in to reply