What is happening is the following issue:
- I have my own overwritten QGraphicsScene.
- I superscribe my tho methods, dragEnterEvent and dropEvent.
- When I drag an image from the browser, for example from chrome, both are executed. (PERFECT)
- 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
- 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
- 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.
dragMoveEvent
also ? A nice example can be found here : link – SyedElec