0
votes

What is happening is the following issue:

  1. I have my own overwritten QGraphicsScene.
  2. I superscribe my tho methods, dragEnterEvent and dropEvent.
  3. When I drag an image from the browser, for example from chrome, both are executed. (PERFECT)
  4. BUT when I drag an image from a local folder, from my local explorer to inside my application, only the dragEnterEvent is executed but not my dropEvent
  5. My approach is that inside my dragEnterEvent I work on the QPixMap creation and local save and inside my dropEvent I would add this image into my QGraphicsScene
  6. I would like to know how to make the dropEvent catch the event and be executed when a local image is dropped inside my scene.

Here is what I have so far:

My dragEnterEvent:

def dragEnterEvent(self, q_graphics_scene_drag_drop_event):
    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())
            data = urllib.request.urlopen(url).read()
            image = QImage()
            image.loadFromData(data)
            self.browser_img = QPixmap(image)
            self.browser_img.save("resources/my_image.png")
            print("LOCAL / CHROME finalized")
        else:
            print("WEBVIEW")
            html = q_graphics_scene_drag_drop_event.mimeData().html()
            matches = re.search('src="([^"]+)"', html)
            url = matches.group()[5:-1]
            print(url)
            data = urllib.request.urlopen(url).read()
            image = QImage()
            image.loadFromData(data)
            self.browser_img = QPixmap(image)
            self.browser_img.save("resources/my_image.png")
            print("WEBVIEW finalized")
    except:

        print('error',sys.exc_info())
        self.update()

    # HERE I CAN USE THE CODE OF THE DROPEVENT, BUT EACH TIME THAT I
    # GET OUT AND GET INSIDE MY SCENE THE IMAGE WILL BE ADDED AGAIN.
    # I DON'T WANT THAT.
    # self.graphics_image = QGraphicsPixmapItem(self.browser_img)
    # self.graphics_image.setFlags(QGraphicsItem.ItemIsSelectable)
    # self.graphics_image.setFlags(QGraphicsItem.ItemIsMovable)
    # self.addItem(self.graphics_image)
    # self.graphics_image.setPos(q_graphics_scene_drag_drop_event.scenePos())

My dropEvent:

def dropEvent(self, event):
    # HERE IS THE WORKING CODE BUT ONLY EXECUTED WHEN IMAGE DRAGGED FROM
    # BROWSER, NOT EXECUTED WHEN FROM A LOCAL FOLDER.
    self.graphics_image = QGraphicsPixmapItem(self.browser_img)
    self.graphics_image.setFlags(QGraphicsItem.ItemIsSelectable)
    self.graphics_image.setFlags(QGraphicsItem.ItemIsMovable)
    self.addItem(self.graphics_image)
    # QGraphicsSceneDragDropEvent.screenPos()
    self.graphics_image.setPos(event.scenePos())
    super(InteractQGraphicsScene, self).dropEvent(event)

And in my view I also setted that drop is accepted, using:

self.setAcceptDrops(True)

ALLL THE CODE WORKS PERFECTLY, URL REQUISITION, IMAGE CREATION, IMAGE ADDING. AGAIN, THE ONLY PROBLEM IS THE dragEvent NOT BEING EXECUTED WHEN FROM LOCAL FOLDER.

1
did you try implementing the dragMoveEvent also ? A nice example can be found here : linkSyedElec
kinda did, worked fine after many tries :Dyurisnm

1 Answers

1
votes

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()