Vorac
Vorac

Reputation: 9114

How to capture mouse events on an arbitrary QWidget?

I'm building a GUI with Python and Qt. The standard widgets are quite useful and work out of the box. However, I have some ideas about mouse gestures. More precisely: a button or label or text that, after being clicked and the mouse button held pressed, moving the mouse around has special effects.

What is necessary to add mouse support for the following events

to an arbitrary widget?


Right now I am trying to do this by class A, which inherits QAbstractItemView and owns a QWidget. However, nothing works AND

NotImplementedError: QAbstractItemView.verticalOffset() is abstract and must be overridden

Upvotes: 4

Views: 8901

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

QAbstractItemView is not helpful for your task.

You can install event filter on an arbitrary widget using installEventFilter. Your filter class must be inherited from QObject. The documentation contains some useful examples. See QObject::installEventFilter. If you want to install filter on all widgets at once, you can install it for QApplication instance.

Another option is to subclass QWidget (or any other QWidget-derived class) and reimplement its mousePressEvent, mouseMoveEvent and mouseReleaseEvent virtual functions.

Upvotes: 3

Related Questions