Creating a QGraphicsItem in the position of the context menu not positioning properly
-
I have a functionality by which a user can duplicate a selected
QGraphicsItemin the position where the user opened the context menu on theQGraphicsScenewhen they click the duplicate action in the context menu. I am having trouble setting the positionQgraphicsPolygonItemin particular.Below is a sample code of what the code looks like.
Note
clickPointis a private data member inCustomSceneFor the
contextMenuEventvoid CustomScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { if (cursorType == CursorType::Select) { QMenu menu; menu.addAction(duplicateAction); clickPoint = event->scenePos(); menu.exec(event->screenPos()); } }For the function attached to
duplicateActionvoid CustomScene::duplicateSelectedItemInPlace() { QGraphicsItem *selectedItem = ImageEditor::selectedItems().at(0); QGraphicsPolygonItem *polygonItem = qgraphicsitem_cast<QGraphicsPolygonItem *>(selectedItem); QPolygonF toDupPolygon = polygonItem->polygon(); QGraphicsPolygonItem *polygon = ImageEditor::addPolygon(toDupPolygon); polygon->setPos(clickPoint); }The newly created
QGraphicsPolygonItemappears way off from where the context menu was. I'm assuming this is because setPos is setting the position in relation the item instead of the scene, but I'm not too sure. Any solution to this ? -
@amansahil
Check after setting the position if polygon->scenePos() - clickPoint == (0, 0). If it is, there's a problem with the event->scenePos(), and if not, there's a problem with how the polygon position is being set.The right conversion would be
polygon->setPos( clickPoint - polygonParent->scenePos() )mapFromScene() is better than mapToScene() since you have the coordinates in the scene frame and you want them in local coordinates. But it still isn't quite right! You specify position in the local parent coordinates, not the local item frame. So that approach to use is
polygon->setPos( polygonParent->mapFromScene(clickPoint) ) -
Looks like you are not converting co-ordinates to scene co-ordinates. Converting to appropriate scene co-ords should solve your problem.
-
Looks like you are not converting co-ordinates to scene co-ordinates. Converting to appropriate scene co-ords should solve your problem.
@dheerendra How can I do this ? I’ve tried mapToScene and it does not work
-
@dheerendra How can I do this ? I’ve tried mapToScene and it does not work
@amansahil
Define " it does not work" ? Show code? -
@amansahil
Define " it does not work" ? Show code? -
@amansahil
Check after setting the position if polygon->scenePos() - clickPoint == (0, 0). If it is, there's a problem with the event->scenePos(), and if not, there's a problem with how the polygon position is being set.The right conversion would be
polygon->setPos( clickPoint - polygonParent->scenePos() )mapFromScene() is better than mapToScene() since you have the coordinates in the scene frame and you want them in local coordinates. But it still isn't quite right! You specify position in the local parent coordinates, not the local item frame. So that approach to use is
polygon->setPos( polygonParent->mapFromScene(clickPoint) )