如何覆盖QDialog.accept()?
How to overwrite QDialog.accept()?
我有一个主窗口,它在按下按钮时打开一个对话框。我想覆盖对话框的接受功能,这样我就可以实现一些应该在对话框被接受之前执行的自定义功能。这是我的主窗口的示例代码。
from PySide2 import QtCore, QtGui, QtWidgets
from add_new import Add_new_dialog
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 100)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushNew_Bed = QtWidgets.QPushButton(self.centralwidget)
self.pushNew_Bed.setObjectName("pushNew_Bed")
self.pushNew_Bed.resize(QtCore.QSize(500,100))
self.pushNew_Bed.clicked.connect(self.on_add_new_clicked)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushNew_Bed.setText(_translate("MainWindow", "Add New Bed"))
def on_add_new_clicked(self):
Dialog = QtWidgets.QDialog()
Dialog.ui = Add_new_dialog()
Dialog.ui.setupUi(Dialog)
#dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
retValue = Dialog.exec_()
if retValue == 1:
print("Accepted")#dialog.calendarWidget.selectedDate())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这是对话框。
from PySide2 import QtCore, QtGui, QtWidgets
import sys
class Add_new_dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
Dialog.setMinimumSize(QtCore.QSize(400, 300))
Dialog.setMaximumSize(QtCore.QSize(400, 300))
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
#self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.accepted.connect(self.custom_func)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
def custom_func(self):
Dialog.accept()
print("custom func")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Add_new_dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
如果我独立启动对话框,一切都会按预期进行。但是,如果我从主窗口调用它,可以预见的是,我会得到一个错误。
Dialog.accept()
NameError: name 'Dialog' is not defined
我尝试将以下代码添加到对话框中以覆盖接受函数:
def accept(self):
self.custom_func()
Dialog.done(QtWidgets.QDialog.Accepted)
并将按钮绑定到该函数
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(Dialog.reject)
同样,不出所料,我得到了相同的结果。阅读文档并没有多大帮助。非常感谢任何帮助,对于这个笨拙的问题,我深表歉意,但我是 gui 的初学者。提前谢谢你。
您不应修改 Qt Designer 生成的 class,因此您必须重新生成 .py(有关更多信息,请阅读 )所以我假设 class Ui_MainWindow
和 Add_new_dialog
分别属于文件 mainwindow_ui.py
和 add_new_dialog_ui.py
。
考虑到以上,你应该覆盖继承QDialog的class的方法:
main.py
from PySide2 import QtCore, QtGui, QtWidgets
from mainwindow_ui import Ui_MainWindow
from add_new_dialog_ui import Add_new_dialog
class AddDialog(QtWidgets.QDialog, Add_new_dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
def accept(self):
print("custom func")
super().accept()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pushNew_Bed.clicked.connect(self.on_add_new_clicked)
def on_add_new_clicked(self):
dialog = AddDialog()
retValue = dialog.exec_()
if retValue == QtWidgets.QDialog.Accepted:
print("Accepted")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
我有一个主窗口,它在按下按钮时打开一个对话框。我想覆盖对话框的接受功能,这样我就可以实现一些应该在对话框被接受之前执行的自定义功能。这是我的主窗口的示例代码。
from PySide2 import QtCore, QtGui, QtWidgets
from add_new import Add_new_dialog
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 100)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushNew_Bed = QtWidgets.QPushButton(self.centralwidget)
self.pushNew_Bed.setObjectName("pushNew_Bed")
self.pushNew_Bed.resize(QtCore.QSize(500,100))
self.pushNew_Bed.clicked.connect(self.on_add_new_clicked)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushNew_Bed.setText(_translate("MainWindow", "Add New Bed"))
def on_add_new_clicked(self):
Dialog = QtWidgets.QDialog()
Dialog.ui = Add_new_dialog()
Dialog.ui.setupUi(Dialog)
#dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
retValue = Dialog.exec_()
if retValue == 1:
print("Accepted")#dialog.calendarWidget.selectedDate())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这是对话框。
from PySide2 import QtCore, QtGui, QtWidgets
import sys
class Add_new_dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
Dialog.setMinimumSize(QtCore.QSize(400, 300))
Dialog.setMaximumSize(QtCore.QSize(400, 300))
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
#self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.accepted.connect(self.custom_func)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
def custom_func(self):
Dialog.accept()
print("custom func")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Add_new_dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
如果我独立启动对话框,一切都会按预期进行。但是,如果我从主窗口调用它,可以预见的是,我会得到一个错误。
Dialog.accept()
NameError: name 'Dialog' is not defined
我尝试将以下代码添加到对话框中以覆盖接受函数:
def accept(self):
self.custom_func()
Dialog.done(QtWidgets.QDialog.Accepted)
并将按钮绑定到该函数
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(Dialog.reject)
同样,不出所料,我得到了相同的结果。阅读文档并没有多大帮助。非常感谢任何帮助,对于这个笨拙的问题,我深表歉意,但我是 gui 的初学者。提前谢谢你。
您不应修改 Qt Designer 生成的 class,因此您必须重新生成 .py(有关更多信息,请阅读 Ui_MainWindow
和 Add_new_dialog
分别属于文件 mainwindow_ui.py
和 add_new_dialog_ui.py
。
考虑到以上,你应该覆盖继承QDialog的class的方法:
main.py
from PySide2 import QtCore, QtGui, QtWidgets
from mainwindow_ui import Ui_MainWindow
from add_new_dialog_ui import Add_new_dialog
class AddDialog(QtWidgets.QDialog, Add_new_dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
def accept(self):
print("custom func")
super().accept()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pushNew_Bed.clicked.connect(self.on_add_new_clicked)
def on_add_new_clicked(self):
dialog = AddDialog()
retValue = dialog.exec_()
if retValue == QtWidgets.QDialog.Accepted:
print("Accepted")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())