Hello,

I have a small app that uses QGraphicsScene. I have several objects that have their own "selection handles", (small rectangles) which are children of the main objects. I'm trapping the mouseMoveEvents for the selection handles, but it seems that for the very first event, the event->pos() function returns scene coordinates instead of item coordinates. This results in the selection handle jumping whenever the user tries to move it. Here is the code:

Qt Code:
  1. #include "StdAfx.h"
  2. #include "SelectionHandle.h"
  3. #include "DrawingObject.h"
  4. #include <QGraphicsScene>
  5. #include <QGraphicsSceneMouseEvent>
  6. #include <QApplication>
  7.  
  8.  
  9. CSelectionHandle::CSelectionHandle(qreal x, qreal y, int handleId, CDrawingObject* pParent, QGraphicsItem* pGraphicsParent)
  10. : m_pParent(pParent), m_pGraphicsParent(pGraphicsParent)
  11. {
  12. Q_ASSERT(pParent != NULL);
  13. Q_ASSERT(pGraphicsParent != NULL);
  14.  
  15. m_Id = handleId;
  16. m_bHandleMoving = false;
  17.  
  18. // Create the graphics object for the handle
  19. setRect(x-2, y-2, 4, 4);
  20. pParent->m_pScene->addItem(this);
  21.  
  22. setBrush(QBrush(QColor("black")));
  23. setParentItem(pGraphicsParent);
  24. }
  25.  
  26.  
  27. CSelectionHandle::~CSelectionHandle(void)
  28. {
  29. }
  30.  
  31. void CSelectionHandle::mousePressEvent( QGraphicsSceneMouseEvent *event )
  32. {
  33. m_bHandleMoving = true;
  34. }
  35.  
  36. void CSelectionHandle::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
  37. {
  38. QGraphicsItem::mouseMoveEvent(event);
  39.  
  40. if (m_bHandleMoving)
  41. {
  42. QPointF itemPos = pos();
  43. QPointF eventPos, eventParentPos, eventScenePos;
  44.  
  45. eventPos = event->pos();
  46. eventParentPos = mapToParent(eventPos);
  47. eventScenePos = mapToScene(eventPos);
  48.  
  49. prepareGeometryChange();
  50. setPos(eventParentPos);
  51.  
  52. m_pParent->MoveHandle(m_Id, eventParentPos);
  53. }
  54. else
  55. {
  56. QApplication::beep();
  57. }
  58. }
  59.  
  60. void CSelectionHandle::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
  61. {
  62. m_bHandleMoving = false;
  63. }
To copy to clipboard, switch view to plain text mode 

After that first mouse move message goes through, the mouse move messages that follow are in item coordinates, as expected. Can anyone tell me what I am doing wrong here?

Thanks
draftpunk