阻止并隐藏 QDialog:exec_() 的替代方案?
在我基于 Qt 的应用程序(使用 PyQt 4.8.6 构建)中,我有一个类是 QtGui.QDialog 的子类:
class ModelDialog(QtGui.QDialog):
...
当我运行应用程序的用户界面时,我可以像这样显示 QDialog:
def main():
app = QtGui.QApplication(sys.argv)
dialog = ModelDialog()
dialog.exec_()
根据 Qt 文档 和 PyQt 文档,exec_() 是此 QDialog 的阻止函数,它默认为模态窗口(根据定义,它阻止用户与应用程序内的任何其他窗口交互)。这正是正常情况下发生的情况。
然而,最近我一直在研究一种使用所有输入值的默认值来调用整个 QApplication 的方法,而不是要求用户提供任何输入。除了一个方面之外,应用程序的行为与预期一致:调用dialog.exec_() 会导致显示模式对话框。
我能找到的唯一解决方法是捕获 showEvent 函数并立即隐藏窗口,但这仍然允许 QDialog 对象瞬间显示:
class ModelDialog(QtGui.QDialog):
...
def showEvent(self, data=None):
self.hide()
有没有办法防止模态窗口完全显示,同时继续阻止主事件循环?我希望有类似的东西:(
def main():
app = QtGui.QApplication(sys.argv)
dialog = ModelDialog()
dialog.setHideNoMatterWhat(True)
dialog.exec_()
为此,我尝试使用 QWidget.setVisible(False),但是 dialog.exec_()
将对话框设置为无论如何都是可见的,这是根据 Qt 文档 所期望的)
In my Qt-based application (built using PyQt 4.8.6), I have a class that is a subclass of QtGui.QDialog:
class ModelDialog(QtGui.QDialog):
...
When I run the application's user interface, I can display the QDialog like so:
def main():
app = QtGui.QApplication(sys.argv)
dialog = ModelDialog()
dialog.exec_()
According to the Qt docs and the PyQt docs, exec_()
is a blocking function for this QDialog, which defaults to a modal window (which by definition prevents the user from interacting with any other windows within the application). This is exactly what happens under normal circumstances.
Recently, however, I've been working on a way to call through the entire QApplication using defaults for all input values, and not asking the user for any input. The application behaves as expected except for one single aspect: calling dialog.exec_()
causes the modal dialog to be shown.
The only workaround I've been able to find has been to catch the showEvent
function and to promptly hide the window, but this still allows the QDialog object to be shown for a split second:
class ModelDialog(QtGui.QDialog):
...
def showEvent(self, data=None):
self.hide()
Is there a way to prevent the modal window from being shown altogether, while continuing to block the main event loop? I'd love for there to be something like:
def main():
app = QtGui.QApplication(sys.argv)
dialog = ModelDialog()
dialog.setHideNoMatterWhat(True)
dialog.exec_()
(to that end, I tried using QWidget.setVisible(False)
, but dialog.exec_()
sets the dialog to be visible anyways, which is expected according to the Qt docs)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
app.exec_()
而不是dialog.exec_()
。Use
app.exec_()
instead ofdialog.exec_()
.