Results 1 to 14 of 14

Thread: QGraphicsItem mouse events

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QGraphicsItem mouse events

    Hi,

    I have some QGraphicsItems on my scene. I'am percepting mouse events in the QGraphicsView class. Once I try to reimplement mouseMoveEvent in one of my QGraphicsItem, it does't do anything.

    Any ideas?

    Regards.

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QGraphicsItem mouse events


  3. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem mouse events

    Well, I don't wanna percept hover actually.
    I want to percept mouse move or press when the cursor is moved over the item for example.

    Regards.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItem mouse events

    In order for a graphics item to receive mouse move events, it must first accept the mouse press event.
    J-P Nurmi

  5. #5
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem mouse events

    Hi again,

    I reimplemented mouseMoveEvent and mousePressEvent in the QGraphicsView class. From QGraphicsView::mousePressEvent(), I can handle which item is pressed by calling ...items(QPointF). But I want to handle it from the QGraphicsItem class. To do this I reimplemented mousePressEvent() again in the QGraphicsItem class but I couldn't get any respond upon any mouse presses although I first reimplemented mousePressEvent.

    Regards
    Last edited by zgulser; 10th February 2009 at 13:34.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItem mouse events

    Quote Originally Posted by zgulser View Post
    I reimplemented mouseMoveEvent and mousePressEvent in the QGraphicsView class.
    Why? QGraphicsView is supposed to forward mouse events to the scene, which in turn delivers them to those items that they belong to. Custom mouse event handlers in the view tend to break the graphics view framework event handling unless you really now what you're doing.

    From QGraphicsView::mousePressEvent(), I can handle which item is pressed by calling ...items(QPointF).
    The default implementation of QGraphicsScene already does this in an efficient way.

    But I want to handle it from the QGraphicsItem class.
    The default implementation of QGraphicsScene delivers mouse events to those items that they belong to.

    To do this I reimplement mousePressEvent() again in the QGraphicsItem class but I couldn't get any respond upon any mouse presses although I first reipmlement mousePressEvent.
    Probably because the QGraphicsView::mousePressEvent() reimplementation breaks the event delivery chain. Make sure you call the base class implementation.
    J-P Nurmi

  7. #7
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem mouse events

    Quote Originally Posted by jpn View Post
    Why? QGraphicsView is supposed to forward mouse events to the scene, which in turn delivers them to those items that they belong to. Custom mouse event handlers in the view tend to break the graphics view framework event handling unless you really now what you're doing.
    I have a QGraphicsScene object inside my QGraphicsView. So that's why I reimplemented mouse events in QGraphicsView. I did the following

    void QGraphicsView::mousePressEvent(QMouseEvent* mouseEvent)
    {
    QPointF p = mapToScene(mouseEvent.x(), mouseEvent.y());
    itemsList = this->scene.items(p);
    ...
    }

    The default implementation of QGraphicsScene already does this in an efficient way.
    Ok.


    The default implementation of QGraphicsScene delivers mouse events to those items that they belong to.
    How I am handling events in items? By reimplementing mouse events in QGraphicsItems?


    Probably because the QGraphicsView::mousePressEvent() reimplementation breaks the event delivery chain. Make sure you call the base class implementation.
    What do you mean by base class implementation?

    Thanks.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItem mouse events

    Quote Originally Posted by zgulser View Post
    I have a QGraphicsScene object inside my QGraphicsView. So that's why I reimplemented mouse events in QGraphicsView. I did the following

    void QGraphicsView::mousePressEvent(QMouseEvent* mouseEvent)
    {
    QPointF p = mapToScene(mouseEvent.x(), mouseEvent.y());
    itemsList = this->scene.items(p);
    ...
    }
    I don't understand what you mean. What are you trying to achieve with that implementation?

    How I am handling events in items? By reimplementing mouse events in QGraphicsItems?
    Yes.

    What do you mean by base class implementation?
    I meant something like this:
    Qt Code:
    1. void SubClass::function()
    2. {
    3. BaseClass::function(); // <-- call the base class implementation
    4. // ... do your own things
    5. }
    To copy to clipboard, switch view to plain text mode 
    But looking at your current implementation, I'm not convinced at all you should even need to reimplement QGraphicsView::mousePressEvent().
    J-P Nurmi

  9. #9
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem mouse events

    Thanks JPN for your usefull answers.

    What I did is something different. Instead of reimplementing mouse events inside QGraphicsScene, I reimplemented those in QGraphicsView and then used mapToScene in order to achieve convenience. By doing so, I figured out that mouse events are also delivered to the corresponding items.

    By the way, I just missed to write QGraphicsView::mousePressEvent() inside the QGraphicsView::mousePressEvent() { ... }. That's why it didn't work until now.

    King Regards

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItem mouse events

    Quote Originally Posted by zgulser View Post
    What I did is something different. Instead of reimplementing mouse events inside QGraphicsScene, I reimplemented those in QGraphicsView and then used mapToScene in order to achieve convenience.
    But you still haven't given a valid point why are you doing all this in the first place. What's wrong with the default event handling? Maybe there's a better way to do whatever you're trying to do...
    J-P Nurmi

  11. #11
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem mouse events

    Quote Originally Posted by jpn View Post
    But you still haven't given a valid point why are you doing all this in the first place. What's wrong with the default event handling? Maybe there's a better way to do whatever you're trying to do...

    First of all

    I wonder what exactly does the statement in the following mean? This is from the documentation.

    "QGraphicsScene's event propagation architecture schedules scene events for delivery to items, and also manages propagation between items. If the scene receives a mouse press event at a certain position, the scene passes the event on to whichever item is at that position."

    Does it mean that I should handle mouse events first in QGraphicsScene and then in QGraphicsItem class?


    Secondly,

    I guess you mean QGraphicsView by first place. If it is, here is my reason;

    I have a view which has a QGraphicsScene object in it. So I do not create any class from QGraphicsScene. If I created, be sure that I did the way you told above as normally which might be a better way. I'am totally open for such development issues with no argue

    By the way, what's wrong with handling mouse events in the QGraphicsView?
    Regards.
    Last edited by zgulser; 10th February 2009 at 20:25.

Similar Threads

  1. Mouse events in QGraphicsPixmapItem and QGraphicsTextItem
    By manojmka in forum Qt Programming
    Replies: 3
    Last Post: 8th August 2008, 11:38
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. Replies: 9
    Last Post: 22nd June 2008, 22:26
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.