Read about event filters. Instead of implementing the mouse events in the widget, implement the event handling in a separate QObject and install the event filter on all the widgets.
Qt Code:
To copy to clipboard, switch view to plain text mode
Read about event filters. Instead of implementing the mouse events in the widget, implement the event handling in a separate QObject and install the event filter on all the widgets.
Qt Code:
To copy to clipboard, switch view to plain text mode
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
qk (21st February 2017)
Another option might be to define a base class derived from QWidget and DisplayElement; this base class implements your mouse event handlers. ScreenPushButton and your other GUI classes singly-inherit from this one. Be sure to include the Q_OBJECT macro in the definitions of these derived classes.
If you go the event filter route, be sure to read carefully the parts about how to handle events you -aren't- interested in. In the alternative approach I suggested, you only implement event handlers for the events you are interested in; with an event filter, you get them all. On the other hand, you can install multiple event filters on the same object, so you could potentially handle the same event in different ways.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Another option is delegation.
You create a class that handles the common event processing and each of your classes holds an instance of that and delegates the events it received to that instance.
Qt Code:
class DisplayElement { protected: MouseEventHandler m_eventHandler; };To copy to clipboard, switch view to plain text mode
I would presonally keep the serialization methods out of the widget classes.
Cheers,
_
Bookmarks