views:

107

answers:

5

Hi, Sorry for my beginner's question... What is the easiest way to define procedures, which are executed when MousePressEvent or MouseReleaseEvent occurs?

For now I am defining my own class (MyGraphicsView class), which inherits QGraphicsView and I am reimplementing mouse events (which are virtual functions). It works fine but is there any way to solve this problem without a need to define a new class? Can I connect Events with Slots somehow?

Thanks for your help.

+1  A: 

Because the mouse events are protected virtual functions, the easiest approach is exactly what you are doing. I don't see any reason why defining a subclass would cause a problem, so I say stick with what you have.

If you really want to connect the events with slots, you can make your subclass implementation of mousePressEvent(), for example, simply emit mousePressSignal(). Of course, you would also need to declare mousePressSignal() in the signals section of the subclass header.

Justin Ardini
+2  A: 

I can see no problem with overriding QGraphicsView::mousePressEvent and QGraphisView::mouseReleaseEvent. The whole QGraphicsView/QGraphicsScene/QGraphicsItem event-handling concept is built around virtual event-handling functions.

Additionally, also the Qt documentation suggests that "you can provide your own custom scene interaction, by creating a subclass of QGraphicsView, and reimplementing the mouse and key event handlers."

Greg S
+3  A: 

This thread on the Qt Centre forum describes quite well what your options are. Simply put:

  1. Do what you are doing (ie subclassing and reimplementing)

  2. Work with an event filter as described in the thread and link therein.

rubenvb
Marcin, In your case the best way is the 2nd point (Using event filter)
Patrice Bernassola
A: 

From your question,

is there any way to solve this problem without having to define a new class?

The answer is No. You can't.

You have to have inherit from the class that you want to handle the events.

Can I connect Events with Slots somehow?

No. You cannot connect events to slots but only signals can be connected to slots.

The way you are now doing is the way to do.

liaK
Wrong, one can install event filters, or, in the case of QGRaphicsView, also scene event filters.
Frank
@ Frank, Yes one can install event filters but you cannot connect events to the slots.. Within the event handler you can emit the signal but even then only a signal can be connected with a slot and not an event..
liaK
A: 

You can use event filters: See http://doc.qt.nokia.com/4.6/qobject.html#eventFilter Or, for graphics items in particular, http://doc.qt.nokia.com/4.6/qgraphicsitem.html#sceneEventFilter

Note that for the latter, some events change: QMouseEvent becomes QGraphicsSceneMouseEvent for example, so make sure to filter for the right type.

What is easier, depends on the situation. If you have a subclass anyway, reimplementing the virtual method is often more straight-forward than the event filter approach. But if you want to track several widgets from several different classes and need special handling for a certain event, subclassing just for this purpose is tedious and not a good design (and makes e.g. using designer for those widgets harder).

Frank