Drop one or more files into ListWidget or LineEdit

人走茶凉 提交于 2020-04-16 09:56:18

问题


I've been struggling for a good week trying to figure this out myself, and I haven't found any good PyQt5 examples online, so here I am.

I want to be able to do two things:

  1. drag and drop one or more files from an external window (Finder on a Mac or Windows Explorer? I'm blanking on what it is called on Windows) into a ListWidget and

  2. do the same thing, but drop a single file into a LineEdit. I want the full path of the file to appear, so that I can open it up later (not included in this example).

I don't have a problem dragging and dropping from one ListWidget to another, but I can't figure out how to drop files not in the main window. In the code below, I want to drop one or more files in the ListWidget labeled "Files List" or one file into the LineEdit labeled "Line Edit".

Here's my GUI code:

# -*- 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_())

Here's my source code:

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_())

回答1:


The main problem is that you are doing that with the following code:

self.ui.files_listWidget = DragDropList(self)

You are replacing the widgets, and that is false, you are pointing out that the variable "self.ui.files_listWidget" now references another widget but that change will not be applied in the window.

The idea is that "self.ui.files_listWidget" store a custom QListWidget object from the beginning, so for this you must make the following changes:

  1. Create a module where the custom widgets are:

    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()
    
  2. Replace QListWidget and QLineEdit with DropList and DropLineEdit, respectively in file example_dragdrop.py.

    # ...
    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")
            self.files_listWidget = DropList(self.centralwidget)
            self.files_listWidget.setGeometry(QtCore.QRect(40, 40, 511, 81))
            # ...
            self.label_3.setObjectName("label_3")
            self.lineEdit = DropLineEdit(self.centralwidget)
            self.lineEdit.setGeometry(QtCore.QRect(110, 130, 441, 21))
            # ...
    
    
    def retranslateUi(self, MainWindow):
        # ... 
    
  3. Remove unnecessary code from main file:

    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_())
    


来源:https://stackoverflow.com/questions/60663793/drop-one-or-more-files-into-listwidget-or-lineedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!