Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • mouse clicked over widget A while it was visible
  • mouse moved to x, y (at real time)
  • mouse released

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

like image 227
Vorac Avatar asked Oct 26 '25 03:10

Vorac


1 Answers

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.

like image 126
Pavel Strakhov Avatar answered Oct 27 '25 23:10

Pavel Strakhov