[Qt-interest] QgraphicsScene: best way to manage events

Ross Bencina rossb-lists at audiomulch.com
Thu Nov 25 15:54:06 CET 2010


Hi Christian

Sorry to come in late on this...

> I'm trying to implement a kind of drawing tool.
> The user simply select the current tool (select, move, edit; draw
> circle, rectangle, ...) from the main toolbar, and then
> press/move/release the mouse depending on how this tool works.
>
> I have sub-classed the scene, and I have a "Tool" base class.
>
> As each tool will have to manage the mouse events its own way, I'm
> looking at the best way to "forward" the mouse events from the scene
> to the current tool, knowing that I would like later to be able to add
> new tools via plug-ins (so the scene have to know only about the Tool
> base class).
>
> Should I let the tools register/unregister themselves as event filters
> to the scene, or should I forward the events to the current tool in
> the scene's event handler using postEvent()? Or are there any other
> solutions for this typical use-case?

The way I did it was create a subclass of QGraphicsScene that forwards 
events to the registered tool. Actually in my case I implemented the 
Interactor pattern and allow multiple interactors to be registered/active at 
once, which is useful in some cases -- for example you can have an idle 
interactor and spawn different mouse drag Interactors (behaviors) depending 
on what gets clicked on. I think I just created a base Interactor class with 
methods for all the events I was interested in rather than use the standard 
Qt event dispatch mechanism.

I don't remember the exact details but I remember having to do some messing 
around to make sure all the events I wanted got delivered to my Interactors 
correctly since some got "eaten" by the scene by default.

Perhaps using event filters is a good idea.. i'm not sure why I didn't do 
that. Having clearer control over the order of events dispatched to multiple 
interactors might be one reason. Better management of deletion of 
interactors during event dispatch might be another.

Ross.





More information about the Qt-interest-old mailing list