Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating a QGraphicsItem in the position of the context menu not positioning properly
Forum Update on Monday, May 27th 2025

Creating a QGraphicsItem in the position of the context menu not positioning properly

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsitemqgraphicsscene
6 Posts 4 Posters 899 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amansahil
    wrote on last edited by amansahil
    #1

    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 ?

    1 Reply Last reply
    0
    • A amansahil

      @JonB So this what I tried

      polygon->setPos(polygon->mapToScene(clickPoint));
      
      J Offline
      J Offline
      JohnDTill
      wrote on last edited by JohnDTill
      #6

      @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) )
      
      1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        Looks like you are not converting co-ordinates to scene co-ordinates. Converting to appropriate scene co-ords should solve your problem.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        A 1 Reply Last reply
        0
        • dheerendraD dheerendra

          Looks like you are not converting co-ordinates to scene co-ordinates. Converting to appropriate scene co-ords should solve your problem.

          A Offline
          A Offline
          amansahil
          wrote on last edited by amansahil
          #3

          @dheerendra How can I do this ? I’ve tried mapToScene and it does not work

          JonBJ 1 Reply Last reply
          0
          • A amansahil

            @dheerendra How can I do this ? I’ve tried mapToScene and it does not work

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

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

            A 1 Reply Last reply
            0
            • JonBJ JonB

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

              A Offline
              A Offline
              amansahil
              wrote on last edited by
              #5

              @JonB So this what I tried

              polygon->setPos(polygon->mapToScene(clickPoint));
              
              J 1 Reply Last reply
              0
              • A amansahil

                @JonB So this what I tried

                polygon->setPos(polygon->mapToScene(clickPoint));
                
                J Offline
                J Offline
                JohnDTill
                wrote on last edited by JohnDTill
                #6

                @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) )
                
                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved