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

Check if a QPoint lies within a QPolygon



  • I would like to check if a QPoint lies withing a QPolygon (so not just that QPolygon contains a point QPoint, since those are only for the outside). I would really like to check if it's within the surface of a QPolygon.
    I'd like to check on a right mouse press event.



  • @hobbyProgrammer
    What's a QVecor[sic.]?
    Check what your qv_polygons.at(i) actually is, because the error message tells you it thinks it's a QVecor<QPoint>, not a QPolygon. Ah, I see

    A QPolygon object is a QVector<QPoint>.

    You may need something like a <static_cast>(QPolygon)? Or possibly QPolygon(const QVector<QPoint> &points) to get a (new) QPolygon out of the points vector, I don't know.



  • @hobbyProgrammer

    (so not just that QPolygon contains a point QPoint, since those are only for the outside)

    What "outside"? https://doc.qt.io/qt-5/qpolygon.html#containsPoint

    Returns true if the given point is inside the polygon according to the specified fillRule; otherwise returns false.

    That says "inside" .....



  • @JonB I save my qpolygon's in a qvector. When I try qv_polygons.at(i).contains it doesn't seem to work properly, but I also don't get the oppertunity to use containsPoint.

    I get the error:

    const class QVecor<QPoint> had no member named containsPoint;
    

    with outside I mean on the edge/corner points that are stored within QPolygon.



  • @hobbyProgrammer
    What's a QVecor[sic.]?
    Check what your qv_polygons.at(i) actually is, because the error message tells you it thinks it's a QVecor<QPoint>, not a QPolygon. Ah, I see

    A QPolygon object is a QVector<QPoint>.

    You may need something like a <static_cast>(QPolygon)? Or possibly QPolygon(const QVector<QPoint> &points) to get a (new) QPolygon out of the points vector, I don't know.



  • @JonB oh that was really stupid. I was meant to check another QVector of QPolygons...