简体   繁体   中英

QDialog keeps blocking mouse input even after close

PyQt4/5 on OSX El Capitan

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. 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.

Once the process is finished, it should close the QDialog and enable input to the main application again. Because things should happen in the background while the dialog is shown, I am not using exec_() to display the dialog.

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. 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. 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.

What am I doing wrong in automatically closing the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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