I have one QGraphicsEllipseItemthat I want to be mobile and trigger signals when moving. Therefore, I am a QGraphicsEllipseItemsubclass of QGraphicsEllipseItemand QObjectand a itemChangemethod itemChangefor triggering a signal. It all seems to work, but the positions presented seem to refer to the old position of the item. Even if you request an element for its position, it seems only to obtain relative coordinates.
Here is some code to clarify what I did:
class MyGraphicsEllipseItem: public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
public:
MyGraphicsEllipseItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0, QGraphicsScene *scene = 0)
:QGraphicsEllipseItem(x,y,w,h, parent, scene)
{}
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
signals:
void itemMoved(QPointF p);
};
QVariant MyGraphicsEllipseItem::itemChange( GraphicsItemChange change, const QVariant &value )
{
if (change == ItemPositionChange){
emit itemMoved(value.toPointF());
}
return QGraphicsEllipseItem::itemChange(change, value);
}
this is item creation:
MyGraphicsEllipseItem* ellipse = new MyGraphicsEllipseItem(someX, someY, someW, someH);
ellipse->setFlag(QGraphicsItem::ItemIsMovable, true);
ellipse->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
connect(ellipse, SIGNAL(itemMoved(QPointF)), SLOT(on_itemMoved(QPointF)));
graphicsView->scene()->addItem(ellipse);
and slot:
void MainWindow::on_itemMoved( QPointF p)
{
MyGraphicsEllipseItem* el = dynamic_cast<MyGraphicsEllipseItem*>(QObject::sender());
QPointF newPos = el->scenePos();
scaleLbl->setText(QString("(%1, %2) - (%3, %4)").arg(newPos.x()).arg(newPos.y()).arg(p.x()).arg(p.y()));
}
The strange thing is that newposthey are palmost equal, but they contain coordinates relative to the beginning of the movement.
How to find out the current position of a dragged object? Is there any other way to achieve the goal?