在提交触发运行的主表单之前,我使用QMessageBox告诉用户他们输入的字段是否不正确或缺失。当前弹出QMessageBox时,主窗口消失(我认为它会留在它后面但是模态)当你单击OK时,整个应用程序关闭。我看了一些例子,但我无法说出我做错了什么。有人可以帮忙吗?
这是代码的一部分:
def isComplete(self):
complete = True
# check field
variable = self.dlg.ui.txtField.text()
if variable:
# got a non-empty string
else:
complete = False
msgBox = QtGui.QMessageBox()
msgBox.setText("Please fill in all required fields")
msgBox.exec_()
return complete
def run(self):
# show dialog
self.dlg.show()
# run the dialog event loop
result = self.dlg.exec_()
# check necessary fields
complete = self.isComplete()
# see if OK was pressed and fields are complete
if (result and complete):
self.runCalcs()
答案 0 :(得分:1)
在简单的情况下,您可以使用QMessageBox的静态方法information
,question
,warning
和critical
。如果指定 parent arg,它将是模态的:
def isComplete(self):
complete = True
# check field
variable = self.dlg.ui.txtField.text()
if variable:
# got a non-empty string
else:
complete = False
QtGui.QMessageBox.warning(self, "Warning", "Please fill in all required fields")
return complete
def run(self):
# show dialog
self.dlg.show()
# run the dialog event loop
result = self.dlg.exec_()
# check necessary fields
complete = self.isComplete()
# see if OK was pressed and fields are complete
if (result and complete):
self.runCalcs()