Haussem ChƏdly
Haussem ChƏdly

Reputation: 279

Block QMainWindow while child widget is alive , pyqt

I want when the user press the button a form will appear after MainWindow is blocked pending form filling

Upvotes: 4

Views: 16287

Answers (6)

ekhumoro
ekhumoro

Reputation: 120698

You can use a QDialog and show it using exec, which will block the rest of the application until it is closed. The return value of exec also tells you whether the form was closed without committing changes (i.e. cancelled).

Here is a simple demo script that shows how to use a QDialog:

from PyQt5 import QtWidgets, QtCore
# from PyQt6 import QtWidgets, QtCore

class Dialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.checkbox1 = QtWidgets.QCheckBox('Option one')
        self.checkbox2 = QtWidgets.QCheckBox('Option two')
        self.buttonOk = QtWidgets.QPushButton('Ok')
        self.buttonOk.clicked.connect(self.accept)
        self.buttonCancel = QtWidgets.QPushButton('Cancel')
        self.buttonCancel.clicked.connect(self.reject)
        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.checkbox1, 0, 0, 1, 2)
        layout.addWidget(self.checkbox2, 1, 0, 1, 2)
        layout.addWidget(self.buttonOk, 2, 0)
        layout.addWidget(self.buttonCancel, 2, 1)

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        widget = QtWidgets.QWidget()
        layout = QtWidgets.QVBoxLayout(widget)
        self.button = QtWidgets.QPushButton('Show Dialog')
        self.button.clicked.connect(self.handleButton)
        layout.addWidget(self.button)
        self.setCentralWidget(widget)

    def handleButton(self):
        dialog = Dialog(self)
        if dialog.exec_() == QtWidgets.QDialog.DialogCode.Accepted:
            print('Option one:', dialog.checkbox1.isChecked())
            print('Option two:', dialog.checkbox2.isChecked())
        else:
            print('Cancelled')
        dialog.deleteLater()

if __name__ == '__main__':

    app = QtWidgets.QApplication(['Test'])
    window = Window()
    window.setGeometry(500, 300, 200, 100)
    window.show()
    app.exec()

Upvotes: 10

Xavi Martínez
Xavi Martínez

Reputation: 2161

This is what you need

self.setWindowModality(QtCore.Qt.ApplicationModal)

Upvotes: 3

Haussem ChƏdly
Haussem ChƏdly

Reputation: 279

must create Widget inherits from Qdialog

AjoutArBase, AjoutArForm = uic.loadUiType('ajoutArticle.ui')
class AjoutArticle(AjoutArBase,QtGui.QDialog):

Upvotes: 0

You don't need to do anything that the other answers suggest. Using any exec() methods is a surefire way to have bugs, since suddenly your gui code can be reentered. Don't do it.

All you need to do is to set proper window modality before you show it (that's the important part). So:

widget.setWindowModality(Qt.ApplicationModal)
widget.show()

If you wish the window to block only some other window, not the entire application:

widget.setWindowFlags(widget.windowFlags() | Qt.Window)
widget.setParent(otherWindow)
widget.setWindowModality(Qt.WindowModal)
widget.show()

Note that this code is for PyQt4 only, it won't work with Qt 5 as there, the window functionality belongs in a class separate from QWidget.

Upvotes: 14

phyatt
phyatt

Reputation: 19122

Subclass your QDialog or your QWidget with your form, and then connect it like this in the constructory of your main window. You will want to convert this code from c++ to python:

QObject::connect(myPushButton, SIGNAL(clicked), this, SLOT(on_myPushButton()));

//...

void MainWindow::on_myPushButton()
{
    Dialog d;
    int retVal = d.exec();// this is a blocking call
    // Here the user has finished filling out the form.
    // save any data that should be in the form, or respond to the retVal

}

EDIT: Added link to docs on using QDialog::exec()

http://qt-project.org/doc/qt-5/qdialog.html#exec

Hope that helps.

Upvotes: 0

thecreator232
thecreator232

Reputation: 2185

Ok , so you want to block the parent window until the child window has been closed.

dialog = QInputDialog()
dialog.exec_()

Use the exec_() function , it will block until the child window is not closed

for further info :

launch a PyQT window from a main PyQt window, and get the user input?

Python - make a window appear on top of another, block access to other windows until button clicked

Upvotes: 0

Related Questions