本文最后更新于:2018年9月21日 上午
PyQt支持拖入功能。比如拖入文件或者一段文本。
拖入文本
定义了一个label继承自QLabel
,初始化时设置允许拖入。
参见pyqt5-drag-and-drop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| from PyQt5 import QtCore from PyQt5.QtWidgets import QMainWindow, QApplication, QListWidget, QAbstractItemView
class CustomLabel(QLabel): 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())
|
直接调用这个类,将它添加到界面上去。
拖入文件,读取文件路径
这里继承了QLabel。Ui_MainWindow
是用designer画出来的界面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| from PyQt5 import QtCore from PyQt5.QtWidgets import QMainWindow, QApplication, QListWidget, QAbstractItemView
class LabMainWindow(QMainWindow): def __init__(self): super(LabMainWindow, self).__init__() self.ma = Ui_MainWindow() self.ma.setupUi(self) self.drag_in_widget = DragInWidget("Drag in", self)
def _init_ui(self): self.drag_in_widget.move(0, 0)
class DragInWidget(QLabel): def __init__(self, title, parent): super().__init__(title, parent) self.setAcceptDrops(True)
def dragEnterEvent(self, e): if e.mimeData().hasUrls(): e.accept() else: e.ignore()
def dropEvent(self, e): for url in e.mimeData().urls(): path = url.toLocalFile() if os.path.isfile(path): print(path)
|
监听到有效拖动事件后,利用QtCore.pyqtSignal
把信息传递出去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from PyQt5 import QtCore from PyQt5.QtWidgets import QMainWindow, QApplication, QListWidget, QAbstractItemView
class DragInWidget(QtWidgets.QFrame): """ Drag files to this widget """ s_content = QtCore.pyqtSignal(str)
def __init__(self, parent): super(DragInWidget, self).__init__(parent) self.setAcceptDrops(True)
def dragEnterEvent(self, e): if e.mimeData().hasUrls(): e.accept() else: e.ignore()
def dropEvent(self, e): for url in e.mimeData().urls(): path = url.toLocalFile() if os.path.isfile(path): self.s_content.emit(path) print(path)
|
这个Frame可以覆盖在其他控件上面时,会拦截操作
向QListWidget
拖入文件,获取文件路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from PyQt5 import QtCore from PyQt5.QtWidgets import QMainWindow, QApplication, QListWidget, QAbstractItemView
class DragInWidget(QListWidget): """ Drag files to this widget """ s_content = QtCore.pyqtSignal(str)
def __init__(self, parent): super(DragInWidget, self).__init__(parent) self.setAcceptDrops(True) self.setDragDropMode(QAbstractItemView.InternalMove)
def dragEnterEvent(self, e): if e.mimeData().hasUrls(): e.accept() else: e.ignore()
def dropEvent(self, e): for url in e.mimeData().urls(): path = url.toLocalFile() if os.path.isfile(path): self.s_content.emit(path) print(path)
|