draftpunk
10th September 2008, 13:50
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:
#include "StdAfx.h"
#include "SelectionHandle.h"
#include "DrawingObject.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QApplication>
CSelectionHandle::CSelectionHandle(qreal x, qreal y, int handleId, CDrawingObject* pParent, QGraphicsItem* pGraphicsParent)
: m_pParent(pParent), m_pGraphicsParent(pGraphicsParent)
{
Q_ASSERT(pParent != NULL);
Q_ASSERT(pGraphicsParent != NULL);
m_Id = handleId;
m_bHandleMoving = false;
// Create the graphics object for the handle
setRect(x-2, y-2, 4, 4);
pParent->m_pScene->addItem(this);
setBrush(QBrush(QColor("black")));
setParentItem(pGraphicsParent);
}
CSelectionHandle::~CSelectionHandle(void)
{
}
void CSelectionHandle::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
m_bHandleMoving = true;
}
void CSelectionHandle::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
{
QGraphicsItem::mouseMoveEvent(event);
if (m_bHandleMoving)
{
QPointF itemPos = pos();
QPointF eventPos, eventParentPos, eventScenePos;
eventPos = event->pos();
eventParentPos = mapToParent(eventPos);
eventScenePos = mapToScene(eventPos);
prepareGeometryChange();
setPos(eventParentPos);
m_pParent->MoveHandle(m_Id, eventParentPos);
}
else
{
QApplication::beep();
}
}
void CSelectionHandle::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
m_bHandleMoving = false;
}
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
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:
#include "StdAfx.h"
#include "SelectionHandle.h"
#include "DrawingObject.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QApplication>
CSelectionHandle::CSelectionHandle(qreal x, qreal y, int handleId, CDrawingObject* pParent, QGraphicsItem* pGraphicsParent)
: m_pParent(pParent), m_pGraphicsParent(pGraphicsParent)
{
Q_ASSERT(pParent != NULL);
Q_ASSERT(pGraphicsParent != NULL);
m_Id = handleId;
m_bHandleMoving = false;
// Create the graphics object for the handle
setRect(x-2, y-2, 4, 4);
pParent->m_pScene->addItem(this);
setBrush(QBrush(QColor("black")));
setParentItem(pGraphicsParent);
}
CSelectionHandle::~CSelectionHandle(void)
{
}
void CSelectionHandle::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
m_bHandleMoving = true;
}
void CSelectionHandle::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
{
QGraphicsItem::mouseMoveEvent(event);
if (m_bHandleMoving)
{
QPointF itemPos = pos();
QPointF eventPos, eventParentPos, eventScenePos;
eventPos = event->pos();
eventParentPos = mapToParent(eventPos);
eventScenePos = mapToScene(eventPos);
prepareGeometryChange();
setPos(eventParentPos);
m_pParent->MoveHandle(m_Id, eventParentPos);
}
else
{
QApplication::beep();
}
}
void CSelectionHandle::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
m_bHandleMoving = false;
}
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