PDA

View Full Version : QPainter and containsPoint()



Slewman
15th October 2009, 16:26
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



void SmokeDisplay::mousePressEvent(QMouseEvent *event)
{
event->accept;
if (leftPolyM.containsPoint(event->pos(),Qt::WindingFill))
{
leftActive=!leftActive;
}
else if (rightPolyM.containsPoint(event->pos(),Qt::WindingFill))
{
rightActive=!rightActive;
}
else if (bottomPolyM.containsPoint(event->pos(),Qt::WindingFill))
{
bottomActive=!bottomActive;
}
else if (topPolyM.containsPoint(event->pos(),Qt::WindingFill))
{
topActive=!topActive;
}
update();
}


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.

high_flyer
15th October 2009, 18:21
can you show your paintEvent()?

Slewman
15th October 2009, 19:11
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