###前提・実現したいこと
python3.5を使っています。pyqt5を用いてウィンドウに画像をドラッグ&ドロップした時にその画像が表示されるようにしたいです。
###発生している問題・エラーメッセージ
jpg画像をドラッグしてもドロップ可能の表示が出ません。当然ドロップしても何も起こりません。
###該当のソースコード
ウィンドウの左上の、縦、横100ピクセルの領域に画像をドラッグ&ドロップすると、ウィンドウの座標(150,150)の位置からその画像が表示されるコードのつもりで書きました。
python3
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_()
###試したこと
ドラッグ&ドロップを用いたコードの例として、次のコードを参考にしました。
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_()
まだ回答がついていません
会員登録して回答してみよう