PyQt 拖入

2021-02-01 15:04:24 浏览数 (1)

PyQt支持拖入功能。比如拖入文件或者一段文本。

拖入文本

定义了一个label继承自QLabel,初始化时设置允许拖入。

参见pyqt5-drag-and-drop

代码语言:javascript复制
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画出来的界面。

代码语言:javascript复制
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)

QtWidgets.QFrame监听拖入事件

监听到有效拖动事件后,利用QtCore.pyqtSignal把信息传递出去

代码语言:javascript复制
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)  # emit file path

    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拖入事件

QListWidget拖入文件,获取文件路径

代码语言:javascript复制
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)  # emit file path

    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)

0 人点赞