[Qt-interest] QgraphicsScene: best way to manage events
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Fri Nov 26 11:11:50 CET 2010
On 2010-11-25 Ross Ross Bencina wrote:
> ...
> Perhaps using event filters is a good idea..
Okay, I think I confused http://doc.trolltech.com/4.7/qcoreapplication.html#setEventFilter which also receives native messages (MSG, XEvent, ...) with http://doc.trolltech.com/4.7/qobject.html#installEventFilter which I think only receives QEvent based events ("Qt events").
The former is really only useful when you want to handle events such as events coming from a graphics tablet which would otherwise not be supported by Qt, for example - otherwise there is hardly any need to install an event filter in QCoreApplication.
The later allows you to "steal" events from a QObject based object, typically a widget, for example. But you have to distinguish yourself whether the event you have received is a mouse event, keyboard event etc..
So the most common approach, when you are only interested in "normal" events such as mouse movements, button clicks, keyboard modifiers etc., is to simply re-implementing the usual http://doc.trolltech.com/4.7/qgraphicsscene.html#mouseMoveEvent etc., so the Qt event queue figures out what kind of event needs to be send and simply calls the appropriate method in your instance (mousePressEvent, mouseReleaseEvent, ...).
I don't think QGraphicsView/Scene "eats" such events before they deliver it to your method. Unless off course some other widget in your app has focus and already consumes a shortcut, for example, or the aforementioned event filter is installed.
Also don't forget to http://doc.trolltech.com/4.7/qevent.html#accept and event, once you (the tool) dealt with it. Otherwise the Qt event queue will propagate it to the next higher element in the widget hierarchy and it might get consumed twice. And in case of QWidgets (not sure about QGraphicsView/Scene) you also need to explicitly enable "mouse moves" (without buttons pressed): http://doc.trolltech.com/4.7/qwidget.html#mouseTracking-prop if you are interested in those mouse move events.
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list