View Full Version : Defining widget regions sensitive to mouse events?

29th December 2010, 00:33

I have a main window that consists of several widgets stacked on top of each other. The widgets are different shapes and do not visually block each other, so you can see them all separately, however they do overlap. How can I let mouse events actually go to their corresponding widgets? As it stands right now, the top most widget captures all events. I think this makes sense since the widgets technically have the same outer rect(), based on the main window.

So specifically, whereas something like setAttribute(Qt::WA_TransparentForMouseEvents) would make an entire widget receive no mouse events, how can I have a defined region for each widget to receive mouse event, instead of having the whole rect() of the widget be sensitive to events?

29th December 2010, 02:37
If sibling widgets overlap then this is a misdesign of the UI. If you want to stack widgets then really stack them - make one a parent of the other. Otherwise it's not possible to propagate events properly as for widgets the events propagate from the child to the parent (and since a widget can have only one parent, if it overlaps some of its siblings, siblings won't receive the events). Once you do that, reimplement mouse event handlers for your widgets and ignore() the events when they happen in regions you consider transparent for a particular widget. Then the event propagation mechanism will kick in and forward the event to the parent.

Alternatively implement your UI in Graphics View which is probably a better choice here if you really need overlapping items. Event propagation for graphics item is different than for widgets -- there events propagate to items that are "under" the item currently handling the event in the point where the event happens. Moreover graphics items are empty by default so defining shape() properly will immediately get you the behaviour you want.