Mouse events without own class
-
Hello,
I have a subclass of QMainWindow and several standard widgets "inside".
When the user right clicks somewhere, a menu with simple operations like "cut", "copy" and "paste" should pop up. My issue with this is, that the lower level widgets accept all of the QMouseEvents so that my subclass of QMainWindow never gets to see any of them.
The only solution I could think of, would be to create my own class for every kind of widget I use and overwrite their mousePressEvent functions. That would be quite uncomfortable though and I feel like Qt probably offers a better way for this, since it looks like a rather common thing to me. -
Three possible solutions:
don't use the QMainWindow to pop up a context menu but use the proper Qt mechanisms:- by creating a custom context menu (and setting the context menu policy accordingly). React to the customContextMenuRequested signal and pop up that menu
- or by creating QAction(s) for the widgets and set context menu policy accordingly to use the actions as menu items
Have QMainWindow handle events centrally:
- by installing an event filter which achieves what you initially wanted: steal events from subwidgets before they get to them.
-
What do you mean by "proper mechanism"?
So to use Qt "properly", I would have to create a subclass for every widget? Sounds a bit excessive to me. Or did I misunderstand you? -
You did misunderstand me. The "proper" refers to the way of creating a popup menu and showing it. For example with multiple QAction and the appropriate context menu policy.
The not so proper way is what you suggested, hacking the event system to intercept events that actually should reach the individual widgets. -
Sorry for not answering for some time.
I did it with the custom menu thing and it works just fine. Thanks for helping me out.