[PyQt] how to close a dialog during initialisation

David Cortesi davecortesi at gmail.com
Sun Apr 5 16:07:47 BST 2015


I want to close a Qdialog during its initialisation i.e. inside def
__init__.  I do some computing during the initialisation of a dialog.
if they don't return a particular value i wish to close the dialog
inside the init statement is it possible or is there a way i can achieve it.

One way would be to raise an exception inside __init__().

class myOddDialog(QDialog):
    def __init__(parent):
        if something_is_wrong :
            raise ValueError

And then create it inside a try:

    try:
        odd_dialog = myOddDialog(self)
        response = odd_dialog.exec_()
    except:
        response = failure_value

But this is very peculiar. Anything that the dialog could figure out during
__init__(), the parent code could also figure out before it creates the
dialog or shows the dialog. So I would just test for the bad condition in
the main code and only display the dialog (call its .exec_() method) when
the condition was good.

Also be aware of the nice "canned" dialogs available as static methods of
QMessageBox, such as question(), warning() and so on. Read the doc for
QMessageBox, it can save you a lot of effort.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150405/6501ceec/attachment.html>


More information about the PyQt mailing list