PyQt:为什么弹出对话框会阻止其他代码的执行?
我在弹出对话框方面遇到了一个小问题。我有一个组合框,当选项更改时,它会弹出一个带有文本编辑小部件的对话框,执行一些操作并在文本编辑小部件中插入一些文本。
这就是我用于弹出窗口的内容:
def function_1(self):
dialog = QDialog()
dialog.ui = Ui_Dialog_popup()
dialog.ui.setupUi(dialog)
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
我在一个单独的 py 文件中使用 QtDesignere 制作了弹出 gui 代码。
出现弹出对话框,但如果该对话框未关闭,则无法执行其他操作。您知道我该如何处理吗?谢谢。
I am having a little problem with a pop up dialog.I have a combobox,which when the option changes it pops up a dialog with a textedit widget,do some stuff and insert some text in the textedit widget.
This is what i use for the popup:
def function_1(self):
dialog = QDialog()
dialog.ui = Ui_Dialog_popup()
dialog.ui.setupUi(dialog)
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
I have the pop up gui code made in QtDesignere in a separate py file.
The popup dialog appears,but if the dialog is not closed,nothing else can be executed.Do you know how can I deal with this ? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是
QDialog
exec 方法> 设计用于:模式对话框。阅读“模式”和“非模式对话框”部分。如果您不希望该对话框阻止您的主 UI,请调用
show()
而不是exec()
(并检查modal
属性文档)。That's exactly what the
exec
method ofQDialog
is designed for: modal dialogs. Read the "Modal" and "Modeless dialog" sections.If you don't the dialog to block your main UI, call
show()
instead ofexec()
(and check themodal
property documentation).详细说明 Mat 所说的: show() 函数立即返回,并且由于对话框是该函数的本地对话框,因此一旦“function_1”返回,该对象就会被删除。您可能希望使该对话框成为成员对话框或全局对话框(无论哪个适合您的要求),以便该对象保留在内存中。
华泰
Elaborating on what Mat said: The show() function immediately returns, and as dialog is local to this function, the object gets deleted as soon as "function_1" returns. You might want to make the dialog a member or global (whichever suits your requirement) so that the object stays in memory.
HTH
由于您正在设置
WA_DeleteOnClose
窗口属性,我假设您希望在每次调用function_1
方法时创建一个新对话框(这可能是一个好主意) 。如果是这样,解决您的问题的最简单方法(基于您给出的代码)是为您的对话框提供一个父级(因此它保持活动状态),然后显示它 无模式使用
show()
:Since you're setting the
WA_DeleteOnClose
window attribute, I'm assuming you want to create a new dialog every time thefunction_1
method is called (which is probably a good idea).If so, the simplest way to solve your issue (based on the code you've given), is to give your dialog a parent (so it is kept alive), and then display it modelessly using
show()
: