6

I have created a QMainWindow in python with Qt library. When user clicks on the close button, I'm prompting a warning QMessageBox (Yes/No). I want to fully disable main window's functionality until user chooses yes or no -to the second window-. (I want something like "freezing" the main window.) I have tried "self.setEnabled(False)" for the main window, but user could still click on minimize and maximize buttons.

Thank you in advance.

4 Answers 4

5

Try to set the window modality on constructor. Here is an example:

def _init_(self):
    self.setWindowModality(QtCore.Qt.ApplicationModal))
Sign up to request clarification or add additional context in comments.

2 Comments

Behaviour appears to be platform-dependent - neither this nor Simon Hibbs' answer work fully on my system (Ubuntu, budgiewm, PyQt5). Both prevent interaction with elements inside the parent window, but do not prevent minimizing or maximizing the parent window as the OP asked for, or moving the parent window. Both answers work fully (incl preventing min/max/moving) on Windows 10 with the same code.
Nor the .setModal(True) solution, that is.
2

What you want is a Modal dialog. Have a look at the documentation for QDialog and the section in the Detailed Description on Modal dialogs. A Modal dialog takes over the UI so it's the only point of interaction with the user until it's dismissed, while a modeless dialog allows continued interaction with other windows of the app. The docs discuss various ways to make a dialog modal and pros and cons of each.

QDialog

6 Comments

You probably are right, however I can't make it work. I used "setWindowModality(QtCore.Qt.ApplicationModal)" to the Dialog.
When you create the dialog after the user clicks the close button, but before you call show() or exec_(), call dialog.setModal(True) to make the dialog modal.
Still not working. This is what I call, when a specific button is clicked -Dialog=the window, dialog=the python file that has the implementation of Dialog-: Dialog = QtGui.QDialog() ui = dialog.Ui_Dialog() ui.setupUi(Dialog) Dialog.setModal(True) Dialog.exec_()
If you use dialog.setModal() you should open the dialog using show() as per the docs. If you open a dialog using it's exec_() function you don't need to set it as modal, or disable the main window, etc.. You've probably got some unecessery stuff in your code that's messing things up but it's hard to say without more info.
Not working for me. I'm using linux. Can you provide a working example? Thx
|
0

Nicolescu's answer is perfect and working. When you open popup/modal(QWidgets.QWidget) window, and want to disable parent window activiy, until the child is open, just use hist example.

def init(self): self.setWindowModality(QtCore.Qt.ApplicationModal))

1 Comment

Always try adding something new to the answer. your answer is just a repetition of another answer by @Nicolescu
0

the best option for most windows in PyQt5 is just to write self.setDisabled(True), however, this also looks very odd since the window stays the same, yet nothing works. the better option is to just use self.hide(), which makes the window completely invisible

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.