###前提・実現したいこと
python3.5を使っています。pyqt5を用いてウィンドウに画像をドラッグ&ドロップした時にその画像が表示されるようにしたいです。
###発生している問題・エラーメッセージ
jpg画像をドラッグしてもドロップ可能の表示が出ません。当然ドロップしても何も起こりません。
###該当のソースコード
ウィンドウの左上の、縦、横100ピクセルの領域に画像をドラッグ&ドロップすると、ウィンドウの座標(150,150)の位置からその画像が表示されるコードのつもりで書きました。
python3
1import sys 2from PyQt5.QtWidgets import (QPushButton, QWidget, 3 QLineEdit, QApplication) 4from PyQt5.QtCore import * 5from PyQt5.QtGui import * 6from PyQt5.QtWidgets import * 7 8 9 10class Button(QPushButton): 11 12 def __init__(self, title, parent): 13 super().__init__(title, parent) 14 15 self.setAcceptDrops(True) 16 17 18 def dragEnterEvent(self, e): 19 20 if e.mimeData().hasFormat('image/*'): 21 e.accept() 22 else: 23 e.ignore() 24 25 def dropEvent(self, e): 26 self.label.setPixmap(QPixmap(event.mimeData().imageData())) 27 28 29class Example(QWidget): 30 31 def __init__(self): 32 super().__init__() 33 34 self.initUI() 35 36 37 def initUI(self): 38 39 button = Button("",self) 40 button.resize(100,100) 41 button.setIcon(QIcon("gazo1.jpg")) 42 button.setIconSize(QSize(100,100)) 43 button.move(0, 0) 44 45 self.label = QLabel(self) 46 self.label.setPixmap(QPixmap('gazo2.jpg')) 47 self.label.move(150,150) 48 49 self.setWindowTitle('Simple drag & drop') 50 self.setGeometry(300, 300, 300, 300) 51 52 53if __name__ == '__main__': 54 55 app = QApplication(sys.argv) 56 ex = Example() 57 ex.show() 58 app.exec_() 59
###試したこと
ドラッグ&ドロップを用いたコードの例として、次のコードを参考にしました。
import sys from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication) class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) self.setAcceptDrops(True) def dragEnterEvent(self, e): if e.mimeData().hasFormat('text/plain'): e.accept() else: e.ignore() def dropEvent(self, e): self.setText(e.mimeData().text()) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): edit = QLineEdit('', self) edit.setDragEnabled(True) edit.move(30, 65) button = Button("Button", self) button.move(190, 65) self.setWindowTitle('Simple drag & drop') self.setGeometry(300, 300, 300, 150) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() app.exec_()

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/08 00:42