Results 1 to 3 of 3

Thread: Can not capture expected mouse move event from GraphicsItem::sceneEventFilter

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default Can not capture expected mouse move event from GraphicsItem::sceneEventFilter

    Hi Qt experts,

    I can not capture the expected mouse move event from GraphicsItem::sceneEvent().

    I rewrite David w drell's code of "Example of re-sizing a QGraphicsItem using Mouse events."

    The class StateBox inherits from QGraphicsPixmapItem and install the corner members that users can drag the corner to to-size the item.

    These corners have been installed and sceneEventFilter() watch them.

    class Corner : public QGraphicsItem {..}
    class StateBox : public QGraphicsPixmapItem { .. Corner* _corners[8]; ..};

    void StateBox::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
    .. _corners[i]->installSceneEventFilter(this); ..} // then item can watch corner event



    in the event watch filter - sceneEventFilter(QGraphicsItem *watched, QEvent* event),
    I monitor the mouse movement of the corner in the StateBox's coordinate

    Corner *corner = dynamic_cast<Corner*>(watched);
    ...
    QGraphicsSceneMouseEvent * mevent
    = dynamic_cast<QGraphicsSceneMouseEvent*>(event);

    if (event->type() == QEvent::GraphicsSceneMouseMove)
    // dump mevemt->pos();


    However, the mouse movement is very strange ..
    If I drag the bottom-right corner, the position seems located on StateBox's coordinate and corner's coordinate.

    Is it a normal behavior ?

    How can I get the corner's position always in StateBox's ?

    this is my code link
    StateBox StateBox

    this is the mouse moving data form corner 2 (the right-bottom corner)
    example2.png
    " QEvent == 156" corner 2 mousedown QPointF(1, 3) // ? corner' coordinate ?
    " QEvent == 155" corner 2 move QPointF(2, 3) // ? corner's coordinate ?
    " QEvent == 155" corner 2 move QPointF(88, 89) // statebox's coordinate
    " QEvent == 155" corner 2 move QPointF(4, 4) // ? corner's coordinate ?
    " QEvent == 155" corner 2 move QPointF(90, 90) // statebox's coordinate
    " QEvent == 155" corner 2 move QPointF(6, 7)
    " QEvent == 155" corner 2 move QPointF(91, 91)
    " QEvent == 155" corner 2 move QPointF(8, 8)
    " QEvent == 155" corner 2 move QPointF(93, 92)

    Can you help ? Thank you very much.
    Last edited by chiaminhsu; 26th April 2013 at 04:19.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can not capture expected mouse move event from GraphicsItem::sceneEventFilter

    If the Corner instance is a child of StateBox (in other words, when you create the Corner instance, you set its parent to the StateBox instance), then you can use corner->mapToParent( mevent->pos() ) to get the mouse position in StateBox coordinates.

    But you have to check to be sure it is the Corner instance that is sending the event, and not the StateBox. In the list of events you posted, some are from a Corner, some are from the StateBox. To make sure of this, do not use dynamic_cast<> use qgraphicsitem_cast< Corner >( watched ); It will return 0 if the object is not a Corner. To make this work, you must reimplement the QGraphicsItem::type() method for your Corner class.
    Last edited by d_stranz; 27th April 2013 at 00:55.

  3. The following user says thank you to d_stranz for this useful post:

    chiaminhsu (27th April 2013)

  4. #3
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default Re: Can not capture expected mouse move event from GraphicsItem::sceneEventFilter

    Hi,

    I modify the code as you said

    Qt Code:
    1. // overwrite virtual function type() in CornerGrabber
    2. enum { Type = UserType + 1101; } // just some number
    3. virtual int type() const { return Type; }
    4.  
    5. // in sceneEventFilter()
    6. CornerGrabber * corner = qgraphicsitem_cast<CornerGrabber *>(watched);
    7. QPointF p = corner->mapToParent(mevent->pos());
    To copy to clipboard, switch view to plain text mode 

    It works! Thank you very much
    Last edited by chiaminhsu; 27th April 2013 at 07:49.

Similar Threads

  1. Capture mouse event on QHeaderView
    By squidge in forum Qt Programming
    Replies: 10
    Last Post: 10th August 2012, 11:54
  2. Problem with QGraphicsScene mouse move event
    By Nesbit in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2012, 18:12
  3. Replies: 3
    Last Post: 7th January 2012, 08:38
  4. QSystemTrayIcon capture mouse hover event
    By alan in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 19:42
  5. Mouse Move Event
    By merry in forum Newbie
    Replies: 5
    Last Post: 3rd June 2007, 06:26

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.