PDA

View Full Version : Widget collision in QGraphicsScene



Rodrigo Alves
21st November 2012, 05:36
Hi, I'm new to Qt.

So, I'm having an issue that I can't find a way to solve. Let me explain my code:

I have a QWidget called Table. Inside this widget there's a QGraphicsView and a QGraphicsScene inside the QGraphicsView.

I am adding QWidgets called Item to this QGraphicsScene.

First, I wanted to make this Item QWidget transparent, so I used:
item->setAttribute(Qt::WA_TranslucentBackground);

That worked fine! But, I also wanted for the collision detection in the GraphicsScene and the mouse events of this Item widget to ignore the transparent areas. I tried:
item->setAttribute(Qt::WA_TransparentForMouseEvents);

But, that changed nothing. The QWidget is transparent, but it is still detecting all the widget rect. How to fix it?

Thanks.

Rodrigo Alves
21st November 2012, 13:41
Ok guys, I was able to fix it, but I had to redo a lot of things.

Instead of directly adding the QWidget to the QGraphicsScene, now I'm adding it to a QGraphicsProxyWidget.
The QGraphicsProxyWidget implements the mouseMove correctly once I extend the shape() function.

In the shape() function, this is what I have to fix it:

QPainterPath newShape;
newShape.addRegion( QRegion( imageInsideMyWidget.createHeuristicMask()) );
return newShape;

(of course, I'm doing this just once because createHeuristicMask seems quite expensive).

So, that's it:
QPainterPath TableItemDecorator::shape() const
{
return itemShape;
}

Fixed. :)

amleto
21st November 2012, 15:53
well done and nice solution