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

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 898 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 31 Jan 2020, 15:11 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
      31 Jan 2020, 23:31

      @JonB So this what I tried

      polygon->setPos(polygon->mapToScene(clickPoint));
      
      J Offline
      J Offline
      JohnDTill
      wrote on 1 Feb 2020, 01:50 last edited by JohnDTill 2 Jan 2020, 01:52
      #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
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 31 Jan 2020, 16:33 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 31 Jan 2020, 17:01
        0
        • D dheerendra
          31 Jan 2020, 16:33

          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 31 Jan 2020, 17:01 last edited by amansahil
          #3

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

          J 1 Reply Last reply 31 Jan 2020, 19:24
          0
          • A amansahil
            31 Jan 2020, 17:01

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

            J Offline
            J Offline
            JonB
            wrote on 31 Jan 2020, 19:24 last edited by
            #4

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

            A 1 Reply Last reply 31 Jan 2020, 23:31
            0
            • J JonB
              31 Jan 2020, 19:24

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

              A Offline
              A Offline
              amansahil
              wrote on 31 Jan 2020, 23:31 last edited by
              #5

              @JonB So this what I tried

              polygon->setPos(polygon->mapToScene(clickPoint));
              
              J 1 Reply Last reply 1 Feb 2020, 01:50
              0
              • A amansahil
                31 Jan 2020, 23:31

                @JonB So this what I tried

                polygon->setPos(polygon->mapToScene(clickPoint));
                
                J Offline
                J Offline
                JohnDTill
                wrote on 1 Feb 2020, 01:50 last edited by JohnDTill 2 Jan 2020, 01:52
                #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

                4/6

                31 Jan 2020, 19:24

                • Login

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