PyQt5 使用来自另一个文件的 Drop_groupBox()
PyQt5 using Drop_groupBox() from another file
我的 PyQt5-Application 中有一个 groupBox
作为拖放区,用户可以在其中上传文件,dropEvent
打印拖入拖放区的文件名列表,将它们写入 listWidget
。我稍微更改了原始代码 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_())
当我将所有代码都放在一个文件中时,这工作得很好。但现在我的问题是我正在使用 PyQt Designer 设计一个 gui,我想将“gui”文件与具有编程逻辑(编程功能)的文件分开,这样当我在 gui 中更改某些内容并将其转换时python 文件(pyuic
),Designer 中定义的 not 函数不会被覆盖。所以这就是我尝试过的:
mainwindow.py(gui 文件)
# -*- 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_())
我不明白为什么拖放事件没有传递给 groupBox。在文件 'dropfiles_functions.py' 中,我将 'self.ui.groupBox' 更改为 'new Drop_groupBox',但它的功能不起作用。有人可以向我解释为什么会这样以及我做错了什么吗?
为了使其正常工作,您需要将 QtDesigner 中的 groupbox
小部件从 QtWidgets.QGroupBox
升级为 Drop_groupBox
小部件。您可以查看 official Qt documentation 以了解操作方法。如果成功并将 .ui 文件转换为 .py 文件,行
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
应该被替换为
self.groupBox = Drop_groupBox(self.centralwidget)
并且在 .py 文件的底部应该有一个类似于
的导入语句
from dropfiles_functions import Drop_groupBox
下一步是正确设置 MConv
。由于 MConv
继承自 QMainWindow
和 Ui_MainWindow
,您不需要在 QMainWindow.__init__
中创建单独的 Ui_MainWindow
实例。相反,您可以直接引用 MConv.setupUi
和 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_())
我的 PyQt5-Application 中有一个 groupBox
作为拖放区,用户可以在其中上传文件,dropEvent
打印拖入拖放区的文件名列表,将它们写入 listWidget
。我稍微更改了原始代码 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_())
当我将所有代码都放在一个文件中时,这工作得很好。但现在我的问题是我正在使用 PyQt Designer 设计一个 gui,我想将“gui”文件与具有编程逻辑(编程功能)的文件分开,这样当我在 gui 中更改某些内容并将其转换时python 文件(pyuic
),Designer 中定义的 not 函数不会被覆盖。所以这就是我尝试过的:
mainwindow.py(gui 文件)
# -*- 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_())
我不明白为什么拖放事件没有传递给 groupBox。在文件 'dropfiles_functions.py' 中,我将 'self.ui.groupBox' 更改为 'new Drop_groupBox',但它的功能不起作用。有人可以向我解释为什么会这样以及我做错了什么吗?
为了使其正常工作,您需要将 QtDesigner 中的 groupbox
小部件从 QtWidgets.QGroupBox
升级为 Drop_groupBox
小部件。您可以查看 official Qt documentation 以了解操作方法。如果成功并将 .ui 文件转换为 .py 文件,行
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
应该被替换为
self.groupBox = Drop_groupBox(self.centralwidget)
并且在 .py 文件的底部应该有一个类似于
的导入语句from dropfiles_functions import Drop_groupBox
下一步是正确设置 MConv
。由于 MConv
继承自 QMainWindow
和 Ui_MainWindow
,您不需要在 QMainWindow.__init__
中创建单独的 Ui_MainWindow
实例。相反,您可以直接引用 MConv.setupUi
和 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_())