drag-and-droppyqt5qgroupbox

PyQt5 using Drop_groupBox() from another file


I have a groupBox in my PyQt5-Application which serves as a dropzone, where the user can upload files and the dropEvent prints a list of the filenames that were dragged into the dropzone and writes them to the listWidget. I have slightly changed the original code from here:

dropfiles.py

# -*- coding: utf-8 -*-
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

import os
from PyQt5 import QtCore, QtGui, QtWidgets


class Drop_groupBox(QtWidgets.QGroupBox):
    dropped = QtCore.pyqtSignal(list)

    def __init__(self, parent):
        super().__init__(parent)

        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasUrls():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        list_of_files = [url.toLocalFile() for url in e.mimeData().urls() if os.path.isfile(url.toLocalFile())]
        self.dropped.emit(list_of_files)


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(658, 449)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(658, 449))
        MainWindow.setMaximumSize(QtCore.QSize(658, 449))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label_Files = QtWidgets.QLabel(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_Files.sizePolicy().hasHeightForWidth())
        self.label_Files.setSizePolicy(sizePolicy)
        self.label_Files.setMinimumSize(QtCore.QSize(122, 20))
        self.label_Files.setMaximumSize(QtCore.QSize(122, 20))
        self.label_Files.setObjectName("label_Files")
        self.gridLayout.addWidget(self.label_Files, 0, 0, 1, 1)
        self.listWidget_Files = QtWidgets.QListWidget(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.listWidget_Files.sizePolicy().hasHeightForWidth())
        self.listWidget_Files.setSizePolicy(sizePolicy)
        self.listWidget_Files.setMinimumSize(QtCore.QSize(256, 341))
        self.listWidget_Files.setMaximumSize(QtCore.QSize(256, 341))
        self.listWidget_Files.setObjectName("listWidget_Files")
        self.gridLayout.addWidget(self.listWidget_Files, 1, 0, 1, 1)
        self.gridLayout_3.addLayout(self.gridLayout, 0, 0, 1, 1)
        # self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox = Drop_groupBox(self.centralwidget)
        self.groupBox.dropped.connect(self.fill_fileslist)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.groupBox.setMinimumSize(QtCore.QSize(300, 300))
        self.groupBox.setMaximumSize(QtCore.QSize(400, 300))
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
        self.gridLayout_3.addWidget(self.groupBox, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 658, 19))
        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", "TestCase"))
        self.label_Files.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">Selected Files</span></p></body></html>"))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:600;\">Drop Files Here!</span></p></body></html>"))

    def fill_fileslist(self, files_list):
        self.listWidget_Files.addItems(files_list)
        print(files_list)


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

This works perfectly fine, when I have all the code in one file. But now my problem is that I am designing a gui with the PyQt Designer and I want to separate the "gui"-file from the file with the programming logic (programming functions), so that when I change something in the gui and convert it to a python file (with pyuic), the functions not defined in the Designer won't be overwritten. So this is what I have tried:

mainwindow.py (the gui file)

# -*- coding: utf-8 -*-
#
# Created by: PyQt5 UI code generator 5.6
#
# 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(658, 449)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(658, 449))
        MainWindow.setMaximumSize(QtCore.QSize(658, 449))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label_Files = QtWidgets.QLabel(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_Files.sizePolicy().hasHeightForWidth())
        self.label_Files.setSizePolicy(sizePolicy)
        self.label_Files.setMinimumSize(QtCore.QSize(122, 20))
        self.label_Files.setMaximumSize(QtCore.QSize(122, 20))
        self.label_Files.setObjectName("label_Files")
        self.gridLayout.addWidget(self.label_Files, 0, 0, 1, 1)
        self.listWidget_Files = QtWidgets.QListWidget(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.listWidget_Files.sizePolicy().hasHeightForWidth())
        self.listWidget_Files.setSizePolicy(sizePolicy)
        self.listWidget_Files.setMinimumSize(QtCore.QSize(256, 341))
        self.listWidget_Files.setMaximumSize(QtCore.QSize(256, 341))
        self.listWidget_Files.setObjectName("listWidget_Files")
        self.gridLayout.addWidget(self.listWidget_Files, 1, 0, 1, 1)
        self.gridLayout_3.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.groupBox.setMinimumSize(QtCore.QSize(300, 300))
        self.groupBox.setMaximumSize(QtCore.QSize(400, 300))
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
        self.gridLayout_3.addWidget(self.groupBox, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 658, 19))
        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", "TestCase"))
        self.label_Files.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">Selected Files</span></p></body></html>"))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:600;\">Drop Files Here!</span></p></body></html>"))


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

dropfiles_functions.py (the file with the programming logic)

# -*- coding: utf-8 -*-


import os
from PyQt5 import QtCore, QtWidgets
from dropfiles_gui import Ui_MainWindow


class Drop_groupBox(QtWidgets.QGroupBox):
    dropped = QtCore.pyqtSignal(list)

    def __init__(self, parent):
        super().__init__(parent)

        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasUrls():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        list_of_files = [url.toLocalFile() for url in e.mimeData().urls() if os.path.isfile(url.toLocalFile())]
        self.dropped.emit(list_of_files)


class MConv(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.groupBox = Drop_groupBox(self.ui.centralwidget)  # <-- why is this not working?
        self.ui.groupBox.dropped.connect(self.fill_fileslist)    # <-- why is this not working?

    def fill_fileslist(self, files_list):
        self.listWidget_Files.addItems(files_list)
        print(files_list)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = MConv()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I don't understand why the drop event is not passed to the groupBox. In the file 'dropfiles_functions.py' I changed the 'self.ui.groupBox' to the 'new Drop_groupBox', but its funcitonaltiy is not working. Can someone please explain to me why this is the case and what I'm doing wrong?


Solution

  • In order to get this to work, you need to promote your groupbox widget in QtDesigner from a QtWidgets.QGroupBox to a Drop_groupBox widget. You can check the official Qt documentation to find out how. If you succeed and convert the .ui file to a .py file, the line

    self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
    

    should have been replaced by

    self.groupBox = Drop_groupBox(self.centralwidget)
    

    and at the bottom of the .py file there should be an import statement similar to

    from dropfiles_functions import Drop_groupBox
    

    The next step is to correctly setup MConv. Since MConv inherits from both QMainWindow and Ui_MainWindow, you don't need to create a separate instance of Ui_MainWindow in QMainWindow.__init__. Instead you can refer to MConv.setupUi and MConv.groupBox directly, i.e.

    class MConv(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super().__init__(self)
            self.setupUi(self)   # <-- this will define self.groupBox = Drop_groupbox(...) amongst others
            self.groupBox.dropped.connect(self.fill_fileslist)
    
        def fill_fileslist(self, files_list):
            self.listWidget_Files.addItems(files_list)
            print(files_list)
    

    Finally, in the if __name__ == "__main__" block you need to make sure you are using an instance of MConv rather than a standard QMainWindow, i.e.

    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = MConv()
        MainWindow.show()
        sys.exit(app.exec_())