开发者

Best way to deal with child to parent mouse and keyboard events in a gui?

开发者 https://www.devze.com 2023-02-24 01:49 出处:网络
For my Gui I w开发者_C百科ant to use the following system: The way it works is that, if the widget under the mouse does not consume a mouse or kb event, it is passed to that widget\'s parent until it

For my Gui I w开发者_C百科ant to use the following system: The way it works is that, if the widget under the mouse does not consume a mouse or kb event, it is passed to that widget's parent until it is consumed or the desktop is reached.

Just one thing puzzles me about it. Does that mean if a Button, for whatever reason has a Label as one of its children. If I click the label, would that not mean that my button, which is under the label would click (since a label does not consume the mouse), which is undesired in this case. Does that mean I'd have to do if(mouseEvent.source == this){do button stuff} ?

Thanks


If the label is a child widget, then yes, the label will attempt to eat the event, fail (as usually the label doesn't have a handler method) and thus pass the event back up to the button.

Chances are the easiest way to do this is to derive a subclass of the Label class, and override the handle function (assuming this is possible in your toolkit - which it should be in any decent toolkit). You can then use your handle function to capture mouse clicks, and pass any other event back up to the button.

Comparing pointers is probably a bad idea and is slightly dependent on the way the toolkit is implemented - for instance, it might deem the source as the button (because that's what it expects), not the label.

Though I find it highly strange that a Label is a widget in itself.....


It looks like you need a special kind of "Label" which consumes mouse events. You should be able to create a customized widget (derived from Label) that consumes mouse events for this specific case.

If that Label widget is from a third party that cannot be derived/subclassed, you should wrap the Label in another widget: A simple widget that consumes mouse events and has only one child, the original Label.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号