I want to get and show images with Drag&Drop methods of pyqt5. For example,like this image,
I want to make Drag&Drop space and Image-show space.
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget,
QLineEdit, QApplication)
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Button(QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('image/*'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
self.label.setPixmap(QPixmap(event.mimeData().imageData()))
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
button = Button("",self)
button.resize(100,100)
button.setIcon(QIcon("gazo1.jpg"))
button.setIconSize(QSize(100,100))
button.move(0, 0)
self.label = QLabel(self)
self.label.setPixmap(QPixmap('gazo2.jpg'))
self.label.move(150,150)
self.setWindowTitle('Simple drag & drop')
self.setGeometry(300, 300, 300, 300)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
I expect that if I drag&drop jpg-image in upper left space, the image is showed in the middle space. But when I drag jpg image to the upper left space, "drop enabled mark" isn't displayed. So when I drag&drop a image, there is no reaction.