PyQt:为什么弹出对话框会阻止其他代码的执行?

发布于 2024-12-20 18:31:51 字数 415 浏览 2 评论 0原文

我在弹出对话框方面遇到了一个小问题。我有一个组合框,当选项更改时,它会弹出一个带有文本编辑小部件的对话框,执行一些操作并在文本编辑小部件中插入一些文本。

这就是我用于弹出窗口的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-12-27 18:31:51

这正是 QDialogexec 方法> 设计用于:模式对话框。阅读“模式”和“非模式对话框”部分。

如果您不希望该对话框阻止您的主 UI,请调用 show() 而不是 exec() (并检查 modal 属性文档)。

That's exactly what the exec method of QDialog 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 of exec() (and check the modal property documentation).

青衫负雪 2024-12-27 18:31:51

详细说明 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

仅此而已 2024-12-27 18:31:51

由于您正在设置 WA_DeleteOnClose 窗口属性,我假设您希望在每次调用 function_1 方法时创建一个新对话框(这可能是一个好主意) 。

如果是这样,解决您的问题的最简单方法(基于您给出的代码)是为您的对话框提供一个父级(因此它保持活动状态),然后显示它 无模式使用 show()

def function_1(self):
    dialog = QDialog(self)
    dialog.ui = Ui_Dialog_popup()
    dialog.ui.setupUi(dialog)
    dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    dialog.show()

Since you're setting the WA_DeleteOnClose window attribute, I'm assuming you want to create a new dialog every time the function_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():

def function_1(self):
    dialog = QDialog(self)
    dialog.ui = Ui_Dialog_popup()
    dialog.ui.setupUi(dialog)
    dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    dialog.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文