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

QPainterPath closed shape is possible?



  • Hi,
    Currently I am working on a basic graphics editor. As part of my requirements, I am trying to figure out if there is a way to create a QPainterPath (irregular) shape where the first point and the last point are a unique same value inside the path structure.

    I was trying to use the path.lineTo() method, but finally I always get two points with the same coords, but my problem is that I am working on a "nodes" editor tool too, so when I try to edit the key points of the shape, I always can open the shape by separating the first and the last point. My goal is that when I am editing any closed path, the first and the last point behave like one. Think of an ellipse or rectangle, I need a similar behavior.

    Any suggestion? Thanks!


  • Qt Champions 2017

    @xtingray
    Hello,
    Have you tried explicitly closing the path?



  • @kshegunov In fact, I already tried it too but I can keep "reopening" the shape if I edit it. I was thinking of it and I guess that is impossible to actually close a path as I need to, because you always will need to repeat the value of the first point in the last point to create the illusion of a closed path, but behind you always will have two different points (with the same value) within the path data structure.

    Maybe the trick I will try is this: every time I select a shape, I will check if first point == last point, then that path is officially closed, so, if I move first point to somewhere else, then I move last point too (same action applies if I try to move the last point). Just guessing.


  • Qt Champions 2017

    @xtingray
    Okay, this might work. But why are you using QPainterPath for the "logic" (assuming you have straight lines). You could keep a polygon, make the changes to it, and in the paint event use drawPolygon or convert it to a closed path and eventually draw it?



  • @kshegunov I am working on a drawing tool, so most of the objects are irregular/curves, like in any hand-made illustration. Using polygons will make my logic more complex and a little out of context. Working with QPainterPath objects directly, the result is very acceptable.
    I just wanted to implement the "closed path" feature without getting in trouble, just that, because I thought I was missing some kind of specific QPainterPath method, but it seems is not the case.


  • Qt Champions 2017

    @xtingray said:

    I am working on a drawing tool, so most of the objects are irregular/curves, like in any hand-made illustration.

    I see, so you're actually not using straight lines, which explains why a polygon isn't a good option.

    I just wanted to implement the "closed path" feature without getting in trouble, just that, because I thought I was missing some kind of specific QPainterPath method, but it seems is not the case.

    Not to my knowledge, no. A painter path can be quite complex and have multiple subpaths in it (some closed, some not) which makes it a bit burdensome, for a lack of better description, to work with. Unfortunately, nothing better comes to mind.


Log in to reply