MarKS
MarKS

Reputation: 504

QPainterPath toFillPolygon() returns more points than the points used to draw the path itself

I have been trying to draw skeletal points of a human using 18 different joints. So, i decided to use QGraphicsPathItem. I could successfully generate the item which looks something like this: (pardon my drawing skills)

enter image description here

To achieve this i used:

QPainterPath pp;

pp.moveTo(m_points[0]); 
pp.lineTo(m_points[1]); 
pp.lineTo(m_points[2]); 
pp.lineTo(m_points[3]); 
pp.lineTo(m_points[4]); 

pp.moveTo(m_points[1]); 
pp.lineTo(m_points[5]); 
pp.lineTo(m_points[6]); 
pp.lineTo(m_points[7]); 

pp.moveTo(m_points[1]); 
pp.lineTo(m_points[8]); 
pp.lineTo(m_points[9]); 
pp.lineTo(m_points[10]);

pp.moveTo(m_points[1]); 
pp.lineTo(m_points[11]);
pp.lineTo(m_points[12]);
pp.lineTo(m_points[13]);

pp.moveTo(m_points[0]); 
pp.lineTo(m_points[14]);
pp.lineTo(m_points[16]);

pp.moveTo(m_points[0]); 
pp.lineTo(m_points[15]);
pp.lineTo(m_points[17]);

m_item->setPath(pp);

At some point of time when i wish to know the positions of the points, i use:

QPolygonF polygon = m_item->path().toFillPolygon();

this returns me 33 points instead of 18.

Is there a way to get the current positions of those 18 points i started with from the path()?

EDIT 1: After comparing the results of toFillPolygon() for an open polygon (start and end points are different) and closed polygon, i realized in an open polygon (as in my case) toFillPolygon() won't work or returns wrong values.

Upvotes: 0

Views: 408

Answers (1)

MarKS
MarKS

Reputation: 504

After realizing i cannot get points from toFillPolygon() i stumbled on this link. It helped me understand we can not extract points from QPainterPath using toFillPolygon() when our path itself is OPEN POLYGON i.e. starting and ending points are different. So, i used something like this:

    QVector<QPointF> pointsTable;
    QPainterPath path = m_item->path();

    pointsTable.reserve(18);

    // push the point 0 manually
    // since it is created using moveTo()
    // but we need this starting point
    pointsTable.push_back(mapToScene(path.elementAt(0)));

    // iterate through all the elements of the path
    for (int idx = 0; idx < path.elementCount(); ++idx)
    {
        // DO NOT consider moveTo() elements
        // we only need lineTo() elements
        if (!path.elementAt(idx).isMoveTo())
        {
            // push into the container
            pointsTable.push_back(mapToScene(path.elementAt(idx)));
        }
    }

Notice i do not consider the elements moveTo() but only the elements lineTo() except for the first moveTo() element which is my starting position.

So, i can get current position of all the 18 points i initially started with to draw the path.

Upvotes: 0

Related Questions