Results 1 to 7 of 7

Thread: mouseMoveEvent in QGraphicsItem

  1. #1
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default mouseMoveEvent in QGraphicsItem

    Dear All,
    I have re-implemented mouseMoveEvent(QGraphicsSceneMouseEvent *event) in my class which is derived from QGraphicsItem. When i draw an item on scene and move the mouse cursor inside the item, mouseMoveEvent() will not trigger. It will only trigger when i click on the item and move the mouse inside the item (i.e leftclick + move). I have set the setAcceptHoverEvents(true). But still mouseMoveEvent() event doesn't trigger. Can anyone tell how can i resolve this problem such that, mouse move on item trigger mouseMoveEvent(). Following is my code

    Qt Code:
    1. CMyItem::CMyItem(QPointF center, QSizeF size)
    2. {
    3. m_Rectangle.setSize(size);
    4. m_Rectangle.moveTopLeft(center);
    5. setZValue(((int)m_Rectangle.top() + (int)m_Rectangle.left())% 2);
    6.  
    7. setFlags(ItemIsSelectable | ItemIsMovable);
    8. setAcceptHoverEvents(true);
    9. }
    10.  
    11. CMyItem::~CMyItem()
    12. {
    13.  
    14. }
    15.  
    16. QRectF CMyItem::boundingRect() const
    17. {
    18. return QRectF(0, 0, 200, 100);
    19. }
    20.  
    21. QPainterPath CMyItem::shape() const
    22. {
    23. path.addRect(0, 0, 200, 100);
    24. return path;
    25. }
    26.  
    27. void CMyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    28. {
    29. Q_UNUSED(widget);
    30. //QBrush brush(
    31. QPen pen;
    32. pen.setColor(Qt::black);
    33. painter->setPen(pen);
    34. painter->drawRect(m_Rectangle);
    35. }
    36.  
    37. void CMyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    38. {
    39. QGraphicsItem::mousePressEvent(event);
    40. update();
    41. event->accept();
    42.  
    43. }
    44.  
    45. void CMyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    46. {
    47. QGraphicsItem::mouseMoveEvent(event);
    48. }
    49.  
    50. void CMyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    51. {
    52. QGraphicsItem::mouseReleaseEvent(event);
    53. update();
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 6th July 2009 at 12:31. Reason: missing [code] tags

  2. #2
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouseMoveEvent in QGraphicsItem

    Any answer for this please????

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouseMoveEvent in QGraphicsItem

    Enable mouse tracking for the view's viewport.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    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: mouseMoveEvent in QGraphicsItem

    Hi,

    this problem was discussed couple of times before in the forum. For example the following,

    http://www.qtcentre.org/forum/f-qt-p...mouseMoveEvent

    but I still got the same problem with it. So good luck

  5. #5
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouseMoveEvent in QGraphicsItem

    i have already used setMouseTracking() for view. but still its not working. In Qt Asistant i have checked description of setAcceptHoverEvents(). i.e.
    Hover events are commonly used to highlight an item when it's entered, and for tracking the mouse cursor as it hovers over the item (equivalent to QWidget::mouseTracking).

    But setAcceptHoverEvents() doesn't act exactly as setMouseTracking()

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouseMoveEvent in QGraphicsItem

    But why do you expect a hover event to trigger a mouse move event? It will trigger hoverMoveEvent()...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    navi1084 (6th July 2009)

  8. #7
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouseMoveEvent in QGraphicsItem

    you are absolutely correct wysota. thank you very much. then i have misunderstood the QGraphicsItem mouseMoveEvent() with QWidget mouseMoveEvent(). Because mouseMoveEvent() itself will be trigger when user hover the mouse on widget.
    My problem is solved. Thank you very much.

Similar Threads

  1. Replies: 5
    Last Post: 31st December 2010, 11:49
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  3. Replies: 1
    Last Post: 25th February 2009, 00:34
  4. Snap-to grid on QGraphicsItem
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 28th April 2008, 16:22
  5. Replies: 6
    Last Post: 30th April 2007, 23:59

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.