发生的事情是以下问题:
以下是我目前的情况:
我的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())
我的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)
在我看来,我还设置了接受,使用:
self.setAcceptDrops(True)
使代码完美运行,网址申请,图片创建,图片添加。 再次,唯一的问题是当地文件夹没有执行的拖延事件。
答案 0 :(得分:1)
我终于得到了问题的答案。
视图上的“举报”:
self.setAcceptDrops(True)
查看:
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)
<强>场景:强>
def dragEnterEvent(self, QGraphicsSceneDragDropEvent):
QGraphicsSceneDragDropEvent.accept()
def dragMoveEvent(self, QGraphicsSceneDragDropEvent):
QGraphicsSceneDragDropEvent.accept()