Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 QGraphicsItem in the position where the user opened the context menu on the QGraphicsScene when they click the duplicate action in the context menu. I am having trouble setting the position QgraphicsPolygonItem in particular.

    Below is a sample code of what the code looks like.

    Note clickPoint is a private data member in CustomScene

    For the contextMenuEvent

    void 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 duplicateAction

    void 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 QGraphicsPolygonItem appears 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) )
    

  • Qt Champions 2017

    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



  • @amansahil
    Define " it does not work" ? Show code?



  • @JonB So this what I tried

    polygon->setPos(polygon->mapToScene(clickPoint));
    


  • @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) )