Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    qgraphicsitem qgraphicsscene
    4
    6
    532
    Loading More Posts
    • 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
      amansahil last edited by amansahil

      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 Reply Quote 0
      • J
        JohnDTill @amansahil last edited by JohnDTill

        @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 Reply Quote 1
        • dheerendra
          dheerendra Qt Champions 2022 last edited by

          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 Reply Quote 0
          • A
            amansahil @dheerendra last edited by amansahil

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

            JonB 1 Reply Last reply Reply Quote 0
            • JonB
              JonB @amansahil last edited by

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

              A 1 Reply Last reply Reply Quote 0
              • A
                amansahil @JonB last edited by

                @JonB So this what I tried

                polygon->setPos(polygon->mapToScene(clickPoint));
                
                J 1 Reply Last reply Reply Quote 0
                • J
                  JohnDTill @amansahil last edited by JohnDTill

                  @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 Reply Quote 1
                  • First post
                    Last post