我的程序有一个MainWindow,其中包含我的库存系统。我添加了一个Dialog窗口,当我点击" Add Item"时弹出窗口。我能够成功打开对话窗口,但我似乎无法关闭它。
当用户尝试关闭对话窗口时,它将显示一个消息框,询问用户是否确实要关闭窗口。目前,我正在使用 self.close() 。它只是关闭我做的消息框以防止意外退出,并且不会关闭对话窗口,除非你使用IDE或任务管理器结束它。
这是我的代码片段:
Main.py
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.db = Database()
self.model = Model(self)
self.ui = MainWindow_ui()
self.ui.setupUi(self)
self.window = Ui_Dialog()
self.ui.addItem.clicked.connect(lambda : self.start_Form())
def start_Form(self):
window = QtGui.QDialog()
self.window.setupUi(window)
self.window.show()
def main():
app = QtGui.QApplication(sys.argv)
window = Main()
window.showMaximized()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
AddItem.py(包含对话框窗口代码)
def getNumber():
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('SELECT seq FROM sqlite_sequence')
itemNumber = c.fetchone()[0]
return int(itemNumber) + 1
class Ui_Dialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(413, 382)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setStandardButtons(
QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Reset)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
def retranslateUi(self, Dialog):
self.itemCode.setText(str(getNumber()))
def accept(self):
row = self.mapper.currentIndex()
self.mapper.submit()
self.main.model.insertRow(row)
self.mapper.setCurrentIndex(row)
self.close()
def reject(self):
ret = QtGui.QMessageBox.question(None, 'Close request', 'Are you sure you want to quit?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if ret == QtGui.QMessageBox.Yes:
self.close()
else:
pass
答案 0 :(得分:1)
您的accept()
和reject()
方法有效地创建了一个无限循环,因为调用close()
将会再次调用这些方法。覆盖虚拟方法时,应使用super
调用基类实现:
class Ui_Dialog(QtGui.QDialog):
...
def accept(self):
...
super(Ui_Dialog, self).accept()
def reject(self):
...
if ret == QtGui.QMessageBox.Yes:
super(Ui_Dialog, self).reject()