PDA

View Full Version : interactive design



pedro74
9th September 2016, 13:51
I have a CustomItem that subclasses QGraphicsItem and draw a pie slice with

painter->drawPie(rect, 0, 45 * 16);

Now I want to handle the mouse click event only when the mouse is over the slice, not over the boundingRect.
How can I do it?

d_stranz
9th September 2016, 17:04
Implement the QGraphicsItem::contains() method for your custom item. When you receive a mouse click, check for the point using that method.

You may not have to actually handle the mouse click - if the QGraphicsItem calls the "contains" method internally, then all you may have to do is to implement it correctly.

pedro74
10th September 2016, 12:03
Thanks d_stranz.
I have found a good solution.
If CustomItem subclasses QGraphicsEllipseItem it works perfectly.

d_stranz
10th September 2016, 19:42
Yes, because your CustomItem class is essentially the same as a QGraphicsEllipseItem that uses a span (i.e. pie slice). Unless your custom class does something special, I don't see any need for it; just use QGraphicsEllipseItem. Notice that QGraphicsEllipseItem reimplements the contains() method...