Linux Tech Blog

Handling GraphicsView events – Part. 1

by fred on Nov.16, 2009, under Git, GraphicsView, Qt

Hi, everyone. It’s been while since I’ve posted something here.

To take out some of the dust from the blog :) ,  I figured we could start with something simple like handling events on the GraphicsView framework.

It works like this: The view (QGraphicsView) receives events and passes them to the scene (QGraphicsScene). In order to pass these events to the scene, the view must first convert these units (pixel coordinates) into scene units (vertex coordinates). This conversion process is transparent, but you access these coordinates through convenience methods like mapFromScene and mapToScene. This also applies for the inverse path (passing events from the scene to the view).

Now, let’s say you want to capture mouse events. To do that, you have to reimplement the mousePressEvent and/or mouseReleaseEvent methods. Depending on your needs, they can be reimplemented from within the QGraphicsItem class (handling single items) or the QGraphicsScene class (handling the whole scene).

Here’s an example (I’ve used the class from the previous post as reference):

void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  qDebug() << "Mouse button clicked at position: "
           << event->pos();
}
 
void CustomItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  qDebug() << "Mouse button released at position: "
           << event->pos();
}

Every time you click on the item, it will display a clicked message, and every time you release the mouse, it will display a released message. These methods are called whenever the desired event happens (Mouse presses, Mouse releases, Mouse moves, Key presses, Key releases, etc).

The QGraphicsSceneMouseEvent is the mouse event object. It’s a similar class to the QMouseEvent class, except that it only handles the QGraphicsView mouse events and not QWidget events.

There are also other handling methods like: mouseMoveEvent and mouseDoubleClickEvent for mouse events or keyPressEvent and keyReleaseEvent for keyboard events. For the keyboard ones, the item must be able to receive keyboard focus (QGraphicsItem::ItemIsFocusable flag set). You just need to reimplement them as in the code above.

And that’s it. I’ve also moved my previous examples (plus this one) to a repository at the Qt Gitorious.

The URL is http://gitorious.org/tech-blog/sources.

The example from this post is under the handling-events dir.


2 Comments for this entry

  • AthifNo Gravatar

    hi there

    i’m a newbiew to Qt and need to figure out how to move items in a scene. Does the framework handle it automatically for us ?

    I tried creating a custom item, set its ItemIsMovable flag and tried moving the mouse on the scene. But with no success :( The item does not move at all.

    Any advice as to what I might be missing ?

    Thanks

  • fredNo Gravatar

    No, the framework does not handle the item movement automatically.
    One way to do this is setting the flag QGraphicsItem::ItemIsMovable.
    Another is reimplementing the mouse handlings methods (Press, Move, Release) with
    calls to the QGraphicsItem::setPos() method, but thats the hard way.

    Have you tried setting the flag on the “handling events” dir on gitorious (http://gitorious.org/tech-blog/sources/trees/master/handling-events)? I’m sure its working.

    What could be happening is that, if you’re reimplementing the QGraphicsItem::mouseMoveEvent() method
    with some handling logic of your own, the flag (ItemIsMovable) may have no effect at all.
    If so, try commenting out the method to see if it works.

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...