Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainterPath::toFillPolygon() on an open polygon returns wrong values
Qt 6.11 is out! See what's new in the release blog

QPainterPath::toFillPolygon() on an open polygon returns wrong values

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MarKSM Offline
    MarKSM Offline
    MarKS
    wrote on last edited by MarKS
    #1

    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)

    body.png

    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.

    I feel this is because my polygon is open (with no same start and end point). I tested toFillPolygon() on a closed polygon it works fine.

    Is there a way to get the current positions of those 18 points i started with from the path()? Or how do we extract all the points in an open polygon path?

    1 Reply Last reply
    0
    • MarKSM Offline
      MarKSM Offline
      MarKS
      wrote on last edited by MarKS
      #4

      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.

      1 Reply Last reply
      1
      • MarKSM Offline
        MarKSM Offline
        MarKS
        wrote on last edited by
        #2

        No comments or replies? None had this issue before?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #3

          Hi
          Did you try with https://doc.qt.io/qt-5/qpainterpath.html#toSubpathPolygons

          The reason you might not get any answers yet
          is that 11 hours is not long enough as users are in different time zones.

          1 Reply Last reply
          2
          • MarKSM Offline
            MarKSM Offline
            MarKS
            wrote on last edited by MarKS
            #4

            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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved