0

我的groupBoxPyQt5-Application 中有一个用作放置区的应用程序,用户可以在其中上传文件并dropEvent打印被拖入放置区的文件名列表并将它们写入listWidget. 我从这里稍微更改了原始代码:

删除文件.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_())

当我将所有代码都放在一个文件中时,这工作得很好。但是现在我的问题是我正在使用 PyQt Designer 设计一个 gui,我想将“gui”文件与具有编程逻辑(编程功能)的文件分开,这样当我在 gui 中更改某些内容并进行转换时到 python 文件(带有pyuic),设计器中未定义的函数不会被覆盖。所以这就是我尝试过的:

mainwindow.py(图形用户界面文件)

# -*- 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(具有编程逻辑的文件)

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

我不明白为什么不将 drop 事件传递给 groupBox。在文件“dropfiles_functions.py”中,我将“self.ui.groupBox”更改为“new Drop_groupBox”,但它的功能不起作用。有人可以向我解释为什么会这样以及我做错了什么吗?

4

1 回答 1

1

为了让它工作,您需要将groupboxQtDesigner 中的小部件从 aQtWidgets.QGroupBox提升为Drop_groupBox小部件。您可以查看官方 Qt 文档以了解如何操作。如果成功并将 .ui 文件转换为 .py 文件,则该行

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

应该被替换为

self.groupBox = Drop_groupBox(self.centralwidget)

并且在 .py 文件的底部应该有一个类似于的导入语句

from dropfiles_functions import Drop_groupBox

下一步是正确设置MConv。由于MConv继承自两者QMainWindowand Ui_MainWindow,您不需要创建单独的Ui_MainWindowin实例QMainWindow.__init__。相反,您可以直接参考MConv.setupUiand MConv.groupBox,即

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)

最后,在if __name__ == "__main__"块中,您需要确保您使用的是实例MConv而不是标准QMainWindow,即

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = MConv()
    MainWindow.show()
    sys.exit(app.exec_())
于 2020-08-28T12:26:19.783 回答