PDA

View Full Version : QGraphicScene item moving itself



thibs
11th March 2017, 03:22
Hello all,

I'm trying to create a 'matrix' of items in a scene, so when the user moves an item and drops, I use the code below to calculate the x index and y index and place the item in the right position. This works fine; however, after I place the item in the new position, when I try to move the item again, the item first returns to the first position and then starts to move. I think the problem is that when I call QGraphicsItem::setPos(), although the item is in the correct visible position, seems that the first drag and drop movement wasn't 'recorded' by Qt. I'm attaching some code to see if helps to understand my problem



bool MyClass::eventFilter(QObject * object, QEvent * event){
QGraphicsSceneMouseEvent *me = (QGraphicsSceneMouseEvent*)event;
switch ((int)event->type()){
case QEvent::GraphicsSceneMouseRelease:
QGraphicsItem *item = scene->itemAt(me->scenePos(),QTransform());
QPointF newPos = ItemNewPosition(item->scenePos());
item->setPos(newPos);
item->update();
this->updateMicroFocus();
this->update();
this->updateGeometry();
return true;
break;
}
return QObject::eventFilter(object, event);
}

QPointF MyClass::ItemNewPosition(QPointF nodePosition)
{
QPointF newPos;
int x = nodePosition.x() / 10;
int y = nodePosition.y() / 50;
newPos.setX( 10 * x);
newPos.setY( 50 * y);
return newPos;
}


Added after 29 minutes:

Got it, I have to return false in my event!