[solved] responding to events without deriving from Qt's widgets
-
It's pretty annoying having to create every time custom widgets where we redefine the event() or mousePressEvent() methods, just for the sake of responding to some events. In my case I only need to respond when the left mouse button is pressed, but I have about 10 QPushButtons that should do different things when pressed... in this case I would end up having 10 different custom widgets derived from QPushButton... looks to me like a waste of time.
So, is it possible to respond to events without having to derive Qt's widgets to custom ones ?
-
Yes, it's possible via event filters.
You create a single QObject that implements "eventFilter()":http://qt-project.org/doc/qt-5.1/qtcore/qobject.html#eventFilter and then install it on whatever number of other objects with "installEventFilter()":http://qt-project.org/doc/qt-5.1/qtcore/qobject.html#installEventFilter
Inside this eventFilter you can check what type of event it was, from which object it came from and act accordingly.But if you want to respond to events like mouse clicks then instead of overwriting mousePressEvent or creating filters you should just connect to clicked(), pressed() or released() signals.
-
Thank you very much! I solved by using the signal-slot connection.
Unfortunately though I haven't found signals for double-click and right-click events. Did I miss them?
-
There's no right click event or double click event simply because these are very unusual actions for buttons.
If you need those you can subclass QPushButton (once!) and emit such custom signals. Then you can connect to different instances of that class like you would with click(). -
That was my idea as well, I just wanted to make sure I didn't miss them.
Thank you very much for help ;)