将一个或多个文件放入 ListWidget 或 LineEdit
Drop one or more files into ListWidget or LineEdit
为了自己解决这个问题,我已经苦苦挣扎了整整一周,但我还没有在网上找到任何好的 PyQt5 示例,所以我来了。
我希望能够做两件事:
从外部 window 拖放一个或多个文件(Mac 或 Windows Explorer 上的 Finder?我不知道它是什么调用 Windows) 到 ListWidget 和
做同样的事情,但将单个文件放入 LineEdit。我希望显示文件的完整路径,以便稍后打开它(不包括在本示例中)。
我在从一个 ListWidget 拖放到另一个 ListWidget 时没有问题,但我不知道如何拖放不在主 window 中的文件。在下面的代码中,我想将一个或多个文件拖放到标记为 "Files List" 的 ListWidget 中,或者将一个文件拖放到标记为 "Line Edit".
的 LineEdit 中
这是我的 GUI 代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'example_dragdrop.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(589, 319)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.files_listWidget = QtWidgets.QListWidget(self.centralwidget)
self.files_listWidget.setGeometry(QtCore.QRect(40, 40, 511, 81))
self.files_listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.files_listWidget.setAlternatingRowColors(True)
self.files_listWidget.setObjectName("files_listWidget")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(260, 20, 91, 21))
self.label_1.setAlignment(QtCore.Qt.AlignCenter)
self.label_1.setObjectName("label_1")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 160, 91, 21))
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.codes_listWidget = QtWidgets.QListWidget(self.centralwidget)
self.codes_listWidget.setGeometry(QtCore.QRect(60, 180, 291, 81))
self.codes_listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.codes_listWidget.setAlternatingRowColors(True)
self.codes_listWidget.setObjectName("codes_listWidget")
self.availCodes_listWidget = QtWidgets.QListWidget(self.centralwidget)
self.availCodes_listWidget.setGeometry(QtCore.QRect(380, 180, 101, 81))
self.availCodes_listWidget.setDragEnabled(True)
self.availCodes_listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
self.availCodes_listWidget.setAlternatingRowColors(True)
self.availCodes_listWidget.setObjectName("availCodes_listWidget")
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(380, 160, 91, 21))
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 130, 441, 21))
self.lineEdit.setObjectName("lineEdit")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 130, 91, 21))
self.label_4.setAlignment(QtCore.Qt.AlignCenter)
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 589, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Drag & Drop List Widget"))
self.label_1.setText(_translate("MainWindow", "Files List"))
self.label_2.setText(_translate("MainWindow", "Codes List"))
__sortingEnabled = self.availCodes_listWidget.isSortingEnabled()
self.availCodes_listWidget.setSortingEnabled(False)
item = self.availCodes_listWidget.item(0)
item.setText(_translate("MainWindow", "Python"))
item = self.availCodes_listWidget.item(1)
item.setText(_translate("MainWindow", "C++"))
item = self.availCodes_listWidget.item(2)
item.setText(_translate("MainWindow", "Ruby"))
item = self.availCodes_listWidget.item(3)
item.setText(_translate("MainWindow", "Perl"))
self.availCodes_listWidget.setSortingEnabled(__sortingEnabled)
self.label_3.setText(_translate("MainWindow", "Codes"))
self.label_4.setText(_translate("MainWindow", "Line Edit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这是我的源代码:
import os
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication, QListWidget
from example_dragdrop import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.files_listWidget = DragDropList(self)
self.ui.files_listWidget.dropped.connect(self.files_dropped)
def files_dropped(self, url_list):
for url in url_list:
if os.path.exists(url):
QtGui.QListWidgetItem(url, self.ui.files_listWidget)
class DragDropList(QListWidget):
dropped = QtCore.pyqtSignal(list)
def __init__(self, type, parent=None):
super(DragDropList, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
urls = []
for url in event.mimeData().urls():
urls.append(str(url.toLocalFile()))
self.dropped.emit(urls)
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
主要问题是您使用以下代码执行此操作:
self.ui.files_listWidget = DragDropList(self)
您正在替换小部件,这是错误的,您指出变量 "self.ui.files_listWidget" 现在引用了另一个小部件,但该更改不会应用到 window。
想法是 "self.ui.files_listWidget" 从一开始就存储一个自定义 QListWidget 对象,因此为此您必须进行以下更改:
创建自定义小部件所在的模块:
dropwidgets.py
from PyQt5 import QtCore, QtWidgets
class DropList(QtWidgets.QListWidget):
def __init__(self, parent=None):
super(DropList, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
md = event.mimeData()
if md.hasUrls():
for url in md.urls():
self.addItem(url.toLocalFile())
event.acceptProposedAction()
class DropLineEdit(QtWidgets.QLineEdit):
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
def dropEvent(self, event):
md = event.mimeData()
if md.hasUrls():
files = []
for url in md.urls():
files.append(url.toLocalFile())
self.setText(" ".join(files))
event.acceptProposedAction()
在文件 example_dragdrop.py.
中分别用 DropList 和 DropLineEdit 替换 QListWidget 和 QLineEdit
# ...
from dropwidgets import DropList, DropLineEdit
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(589, 319)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
<b>self.files_listWidget = DropList(self.centralwidget)</b>
self.files_listWidget.setGeometry(QtCore.QRect(40, 40, 511, 81))
# ...
self.label_3.setObjectName("label_3")
<b>self.lineEdit = DropLineEdit(self.centralwidget)</b>
self.lineEdit.setGeometry(QtCore.QRect(110, 130, 441, 21))
# ...
<pre><code>def retranslateUi(self, MainWindow):
# ...
从主文件中删除不必要的代码:
import sys
from PyQt5 import QtWidgets
from example_dragdrop import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
为了自己解决这个问题,我已经苦苦挣扎了整整一周,但我还没有在网上找到任何好的 PyQt5 示例,所以我来了。
我希望能够做两件事:
从外部 window 拖放一个或多个文件(Mac 或 Windows Explorer 上的 Finder?我不知道它是什么调用 Windows) 到 ListWidget 和
做同样的事情,但将单个文件放入 LineEdit。我希望显示文件的完整路径,以便稍后打开它(不包括在本示例中)。
我在从一个 ListWidget 拖放到另一个 ListWidget 时没有问题,但我不知道如何拖放不在主 window 中的文件。在下面的代码中,我想将一个或多个文件拖放到标记为 "Files List" 的 ListWidget 中,或者将一个文件拖放到标记为 "Line Edit".
的 LineEdit 中这是我的 GUI 代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'example_dragdrop.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(589, 319)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.files_listWidget = QtWidgets.QListWidget(self.centralwidget)
self.files_listWidget.setGeometry(QtCore.QRect(40, 40, 511, 81))
self.files_listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.files_listWidget.setAlternatingRowColors(True)
self.files_listWidget.setObjectName("files_listWidget")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(260, 20, 91, 21))
self.label_1.setAlignment(QtCore.Qt.AlignCenter)
self.label_1.setObjectName("label_1")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 160, 91, 21))
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.codes_listWidget = QtWidgets.QListWidget(self.centralwidget)
self.codes_listWidget.setGeometry(QtCore.QRect(60, 180, 291, 81))
self.codes_listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.codes_listWidget.setAlternatingRowColors(True)
self.codes_listWidget.setObjectName("codes_listWidget")
self.availCodes_listWidget = QtWidgets.QListWidget(self.centralwidget)
self.availCodes_listWidget.setGeometry(QtCore.QRect(380, 180, 101, 81))
self.availCodes_listWidget.setDragEnabled(True)
self.availCodes_listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
self.availCodes_listWidget.setAlternatingRowColors(True)
self.availCodes_listWidget.setObjectName("availCodes_listWidget")
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.availCodes_listWidget.addItem(item)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(380, 160, 91, 21))
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 130, 441, 21))
self.lineEdit.setObjectName("lineEdit")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 130, 91, 21))
self.label_4.setAlignment(QtCore.Qt.AlignCenter)
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 589, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Drag & Drop List Widget"))
self.label_1.setText(_translate("MainWindow", "Files List"))
self.label_2.setText(_translate("MainWindow", "Codes List"))
__sortingEnabled = self.availCodes_listWidget.isSortingEnabled()
self.availCodes_listWidget.setSortingEnabled(False)
item = self.availCodes_listWidget.item(0)
item.setText(_translate("MainWindow", "Python"))
item = self.availCodes_listWidget.item(1)
item.setText(_translate("MainWindow", "C++"))
item = self.availCodes_listWidget.item(2)
item.setText(_translate("MainWindow", "Ruby"))
item = self.availCodes_listWidget.item(3)
item.setText(_translate("MainWindow", "Perl"))
self.availCodes_listWidget.setSortingEnabled(__sortingEnabled)
self.label_3.setText(_translate("MainWindow", "Codes"))
self.label_4.setText(_translate("MainWindow", "Line Edit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这是我的源代码:
import os
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication, QListWidget
from example_dragdrop import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.files_listWidget = DragDropList(self)
self.ui.files_listWidget.dropped.connect(self.files_dropped)
def files_dropped(self, url_list):
for url in url_list:
if os.path.exists(url):
QtGui.QListWidgetItem(url, self.ui.files_listWidget)
class DragDropList(QListWidget):
dropped = QtCore.pyqtSignal(list)
def __init__(self, type, parent=None):
super(DragDropList, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
urls = []
for url in event.mimeData().urls():
urls.append(str(url.toLocalFile()))
self.dropped.emit(urls)
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
主要问题是您使用以下代码执行此操作:
self.ui.files_listWidget = DragDropList(self)
您正在替换小部件,这是错误的,您指出变量 "self.ui.files_listWidget" 现在引用了另一个小部件,但该更改不会应用到 window。
想法是 "self.ui.files_listWidget" 从一开始就存储一个自定义 QListWidget 对象,因此为此您必须进行以下更改:
创建自定义小部件所在的模块:
dropwidgets.py
from PyQt5 import QtCore, QtWidgets class DropList(QtWidgets.QListWidget): def __init__(self, parent=None): super(DropList, self).__init__(parent) self.setAcceptDrops(True) def dragEnterEvent(self, event): if event.mimeData().hasUrls(): event.acceptProposedAction() else: event.ignore() def dragMoveEvent(self, event): if event.mimeData().hasUrls(): event.acceptProposedAction() else: event.ignore() def dropEvent(self, event): md = event.mimeData() if md.hasUrls(): for url in md.urls(): self.addItem(url.toLocalFile()) event.acceptProposedAction() class DropLineEdit(QtWidgets.QLineEdit): def dragEnterEvent(self, event): if event.mimeData().hasUrls(): event.acceptProposedAction() def dropEvent(self, event): md = event.mimeData() if md.hasUrls(): files = [] for url in md.urls(): files.append(url.toLocalFile()) self.setText(" ".join(files)) event.acceptProposedAction()
在文件 example_dragdrop.py.
中分别用 DropList 和 DropLineEdit 替换 QListWidget 和 QLineEdit# ... from dropwidgets import DropList, DropLineEdit class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(589, 319) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") <b>self.files_listWidget = DropList(self.centralwidget)</b> self.files_listWidget.setGeometry(QtCore.QRect(40, 40, 511, 81)) # ... self.label_3.setObjectName("label_3") <b>self.lineEdit = DropLineEdit(self.centralwidget)</b> self.lineEdit.setGeometry(QtCore.QRect(110, 130, 441, 21)) # ... <pre><code>def retranslateUi(self, MainWindow): # ...
从主文件中删除不必要的代码:
import sys from PyQt5 import QtWidgets from example_dragdrop import Ui_MainWindow class MainWindow(QtWidgets.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())