简体   繁体   English

即使关闭后,QDialog仍会阻止鼠标输入

[英]QDialog keeps blocking mouse input even after close

PyQt4/5 on OSX El Capitan OSX El Capitan上的PyQt4 / 5

I have a QMessageBox/QDialog which I want to be modal and want to block input from other GUI items while a process is running. 我有一个QMessageBox / QDialog,我希望它是模态的,并且想在进程运行时阻止来自其他GUI项的输入。 The QDialog should provide the user the option to cancel the said process, but not allow him or her to do anything else with the GUI in the meantime. QDialog应该为用户提供取消上述过程的选项,但同时不允许他或她对GUI进行任何其他操作。

Once the process is finished, it should close the QDialog and enable input to the main application again. 该过程完成后,应关闭QDialog并再次启用对主应用程序的输入。 Because things should happen in the background while the dialog is shown, I am not using exec_() to display the dialog. 因为显示对话框时事情应该在后台发生,所以我没有使用exec_()来显示对话框。

Here is a simple example of my code: 这是我的代码的一个简单示例:

self.openingDialog = QtWidgets.QMessageBox(self.main_window)
self.openingDialog.setText(_(u"Opening experiment. Please wait"))
self.openingDialog.setStandardButtons(QtWidgets.QMessageBox.Cancel)
self.openingDialog.reject.connect(<some_function>)
self.openingDialog.show()
self.openingDialog.raise_()

... [Perform process] ...

self.openingDialog.done(0)
self.openingDialog.close()
self.openingDialog.deleteLater()

Everything works nicely in the sense that the dialog box is shown, and that no interaction is possible with other GUI elements while it is displayed. 在显示对话框的意义上,一切都很好,并且在显示该对话框时,不能与其他GUI元素进行交互。 However, when has process is finished, the dialog box is automatically closed but it is still not possible to interact with other GUI elements afterwards. 但是,完成处理后,对话框将自动关闭,但之后仍然无法与其他GUI元素进行交互。 The GUI does not respond to mouse clicks, menu items are not accessible, and you can't even click the close button, so the application needs to be Force Quit. GUI不响应鼠标单击,无法访问菜单项,甚至不能单击关闭按钮,因此需要强制退出应用程序。

What am I doing wrong in automatically closing the QDialog? 自动关闭QDialog时我做错了什么?

Ok, I have found sort of a workaround, although I don't think it is an elegant solution. 好的,尽管我认为这不是一个很好的解决方案,但我找到了一种解决方法。

If I set the window modality to 'window modal' instead of 'application modal' by using: 如果我通过以下方式将窗口模式设置为“窗口模式”而不是“应用程序模式”:

self.openingDialog.setWindowModality(QtCore.Qt.WindowModal)

then the application regains focus and accessibility after the dialog has been closed by the program. 然后在程序关闭对话框后,应用程序重新获得焦点和可访问性。 Still this doesn't solve the problem when the dialog is an application modal, but for now this serves my needs. 当对话框是应用程序模式时,这仍然不能解决问题,但是现在这可以满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM