Results 1 to 3 of 3

Thread: QPainter and containsPoint()

  1. #1
    Join Date
    Sep 2009
    Posts
    64

    Default QPainter and containsPoint()

    Im trying to make a widget that is 4 triangles put together to for a square, and each triangle is its own polygon... when i click in anyone of the separate triangles, its color toggles between green and red.

    I am able to get the shape drawn exactly how i want it... except now it doesnt behave like i thought it would. I reimplemented a mousePressEvent and used the .containsPoint() function in QPainter to try and compare where i clicked and what is inside the polygon. here is my code

    Qt Code:
    1. void SmokeDisplay::mousePressEvent(QMouseEvent *event)
    2. {
    3. event->accept;
    4. if (leftPolyM.containsPoint(event->pos(),Qt::WindingFill))
    5. {
    6. leftActive=!leftActive;
    7. }
    8. else if (rightPolyM.containsPoint(event->pos(),Qt::WindingFill))
    9. {
    10. rightActive=!rightActive;
    11. }
    12. else if (bottomPolyM.containsPoint(event->pos(),Qt::WindingFill))
    13. {
    14. bottomActive=!bottomActive;
    15. }
    16. else if (topPolyM.containsPoint(event->pos(),Qt::WindingFill))
    17. {
    18. topActive=!topActive;
    19. }
    20. update();
    21. }
    To copy to clipboard, switch view to plain text mode 

    and i select which color to fill the polygons in the paintEvent depending on the bool value of leftActive, rightActive etc...

    so what happens is only the rightPoly and the bottomPoly ever change, but not even when i click inside of them... the rightPoly changes when i click the left half of my topPoly and and bottom changes when i click the top half of my leftPoly... and each change when i am slightly outside of any poly...

    anyone have any suggestions or alternate methods? I attached a picture of what my widget looks like.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter and containsPoint()

    can you show your paintEvent()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2009
    Posts
    64

    Default Re: QPainter and containsPoint()

    i actually got it to work.. the issue was event->pos() was giving me a position relative to the widget where as the polygons were referenced to the center of the widget where i was drawing them...

    thanks for the reply though

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.