1
votes

In my Qt-based application (built using PyQt 4.8.6), I have a class that is a subclass of QtGui.QDialog:

class ModelDialog(QtGui.QDialog):
    ...

When I run the application's user interface, I can display the QDialog like so:

def main():
    app = QtGui.QApplication(sys.argv)
    dialog = ModelDialog()
    dialog.exec_()

According to the Qt docs and the PyQt docs, exec_() is a blocking function for this QDialog, which defaults to a modal window (which by definition prevents the user from interacting with any other windows within the application). This is exactly what happens under normal circumstances.

Recently, however, I've been working on a way to call through the entire QApplication using defaults for all input values, and not asking the user for any input. The application behaves as expected except for one single aspect: calling dialog.exec_() causes the modal dialog to be shown.

The only workaround I've been able to find has been to catch the showEvent function and to promptly hide the window, but this still allows the QDialog object to be shown for a split second:

class ModelDialog(QtGui.QDialog):
    ...
    def showEvent(self, data=None):
        self.hide()

Is there a way to prevent the modal window from being shown altogether, while continuing to block the main event loop? I'd love for there to be something like:

def main():
    app = QtGui.QApplication(sys.argv)
    dialog = ModelDialog()
    dialog.setHideNoMatterWhat(True)
    dialog.exec_()

(to that end, I tried using QWidget.setVisible(False), but dialog.exec_() sets the dialog to be visible anyways, which is expected according to the Qt docs)

1

1 Answers

2
votes

Use app.exec_() instead of dialog.exec_().