相关文章推荐
爱跑步的松鼠  ·  Kaggle经验--时间序列预测真的需要深度 ...·  1 年前    · 
好帅的丝瓜  ·  pyqt5多进程进度条-掘金·  1 年前    · 
干练的枕头  ·  Python 字符串的匹配与替换 - ...·  2 年前    · 
无邪的灌汤包  ·  spark文件读取与保存(scala实现) ...·  2 年前    · 
非常酷的双杠  ·  java ...·  2 年前    · 
Code  ›  PyQt 拖入开发者社区
pyqt qlistwidget
https://cloud.tencent.com/developer/article/1783056
霸气的白开水
2 年前
作者头像
AnRFDev
0 篇文章

PyQt 拖入

前往专栏
腾讯云
备案 控制台
开发者社区
学习
实践
活动
专区
工具
TVP
文章/答案/技术大牛
写文章
社区首页 > 专栏 > AnRFDev > 正文

PyQt 拖入

发布 于 2021-02-01 15:04:24
590 0
举报

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

拖入文本

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

参见 pyqt5-drag-and-drop

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画出来的界面。

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 把信息传递出去

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 拖入文件,获取文件路径

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()
 
推荐文章
爱跑步的松鼠  ·  Kaggle经验--时间序列预测真的需要深度学习模型吗?_变量_Deep_向量
1 年前
好帅的丝瓜  ·  pyqt5多进程进度条-掘金
1 年前
干练的枕头  ·  Python 字符串的匹配与替换 - 二次蓝 - 博客园
2 年前
无邪的灌汤包  ·  spark文件读取与保存(scala实现) - macy_zhang - 博客园
2 年前
非常酷的双杠  ·  java 8:只取年月日的java.util.Date(时分秒清零)对象_date只保留年月日_10km的博客-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号