QGraphicsView Drag&Drop not working when dragging local image to my app

前端 未结 1 1833
陌清茗
陌清茗 2021-01-29 08:38

What is happening is the following issue:

  1. I have my own overwritten QGraphicsScene.
  2. I superscribe my tho methods, dragEnterEvent and drop
相关标签:
1条回答
  • 2021-01-29 09:08

    I finally got the answer to my problem.

    1. I made a small example to see what was the problem.
    2. Just implementing the dragEnterEvent, dropEvent on the view and accepting drop events would work fine.(IF I HAD ONLY THE VIEW AND DEFAULT SCENE)
    3. Since I have my own personalized Scene I had to do the following thing:
    4. To re-implement the dragEnterEvent and the dragMoveEvent, and accept their event in the scene.
    5. And implement my final logic on the view.
    6. Now all work perfectly :D

    "Flag" on the view:

    self.setAcceptDrops(True)
    

    VIEW :

     def dragEnterEvent(self, q_graphics_scene_drag_drop_event):
    
        q_graphics_scene_drag_drop_event.accept()
        q_graphics_scene_drag_drop_event.acceptProposedAction()
        try:
            if q_graphics_scene_drag_drop_event.mimeData().hasUrls():
                print("local chrome")
                url = str(q_graphics_scene_drag_drop_event.mimeData().urls()[0].url())
                NAM_CREATOR.get_nam().request_image(url)
            else:
                print("webview")
                html = q_graphics_scene_drag_drop_event.mimeData().html()
                matches = re.search('src="([^"]+)"', html)
                url = matches.group()[5:-1]
                NAM_CREATOR.get_nam().request_image(url)
        except RuntimeError:
            print('error',sys.exc_info())
            pass
        super(ScreenViewScene, self).dragEnterEvent(
            q_graphics_scene_drag_drop_event)
    
    def dragMoveEvent(self, QDragMoveEvent):
        QDragMoveEvent.accept()
        super(ScreenViewScene, self).dragMoveEvent(QDragMoveEvent)
    
    def dropEvent(self, event):
        print("LEAVE")
        image = QPixmap("resources/browser_images/image_required_browser")
        self.graphics_image = QGraphicsPixmapItem(image)
        self.graphics_image.acceptDrops()
        self.graphics_image.setFlags(QGraphicsItem.ItemIsSelectable)
        self.graphics_image.setFlags(QGraphicsItem.ItemIsMovable)
        self.scene.addItem(self.graphics_image)
        self.graphics_image.setPos(event.pos())
        NAM_CREATOR.get_nam().deleteLater()
        NAM_CREATOR.reset_nam()
    
        super(ScreenViewScene, self).dropEvent(event)
    

    Scene:

    def dragEnterEvent(self, QGraphicsSceneDragDropEvent):
        QGraphicsSceneDragDropEvent.accept()
    
    def dragMoveEvent(self, QGraphicsSceneDragDropEvent):
        QGraphicsSceneDragDropEvent.accept()
    
    0 讨论(0)
提交回复
热议问题