PyQT4: why does QDialog returns from exec_() when setVisible(false)
<p>I'm using Python 2.7 and PyQT4.</p>

<p>I want to hide a modal QDialog instance and later on show it again. However, when dialog.setVisible(false) is called (e.g., using QTimer), the dialog.exec_() call returns (with QDialog.Rejected return value).</p>

<p>However, according to <a href="http://pyqt.sourceforge.net/Docs/PyQt4/qdialog.html#exec" rel="nofollow">http://pyqt.sourceforge.net/Docs/PyQt4/qdialog.html#exec</a>, the _exec() call should block until the user <em>closes</em> the dialog.</p>

<p>Is there a way to hide the dialog without _exec() returning?</p>

<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
from PyQt4 import QtGui, QtCore


class MyDialog(QtGui.QDialog):
    def __init__(self, parent):
        QtGui.QDialog.__init__(self, parent)

    def closeEvent(self, QCloseEvent):
        print "Close Event"

    def hideEvent(self, QHideEvent):
        print "Hide Event"


class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle("Main Window")
        button = QtGui.QPushButton("Press me", self)
        button.clicked.connect(self.run_dialog)

    def run_dialog(self):
        self.dialog = MyDialog(self)
        self.dialog.setModal(True)
        self.dialog.show()
        QtCore.QTimer.singleShot(1000, self.hide_dialog)
        status = self.dialog.exec_()
        print "Dialog exited with status {}".format(status), "::", QtGui.QDialog.Accepted, QtGui.QDialog.Rejected

    def hide_dialog(self):
        self.dialog.setVisible(False)
        # self.dialog.setHidden(True)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())
</code></pre>

<p>PS1: This code prints the following output:</p>

<pre><code>Hide Event
Dialog exited with status 0 :: 1 0
</code></pre>

<p>(the close event is not called).</p>

<p>PS2: For context, I'm trying to implement a SystemTrayIcon that allows to hide and restore a QMainWindow (this part is fine) and possibly its modal QDialog without closing the dialog.</p>

<p>Thanks!</p>

<p>In case anyone is interested, the following code provides a quick-and-dirty way to circunvent the problem for me, although it does not really answer the question.</p>

<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Extension of the QDialog class so that exec_() does not exit when hidden.

Dialogs inheriting will only exit exec_() via close(), reject() or accept()
"""

from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class HideableQDialog(QDialog):
    def __init__(self, *args, **kwargs):
        super(HideableQDialog, self).__init__(*args, **kwargs)
        self._mutex = QMutex()
        self._is_finished = False
        self._finished_condition = QWaitCondition()
        self.finished.connect(self._finish_dialog)

    def _finish_dialog(self):
        self._is_finished = True
        self._finished_condition.wakeOne()

    def close(self):
        super(HideableQDialog, self).close()
        self._finish_dialog()

    def reject(self):
        super(HideableQDialog, self).reject()
        self._finish_dialog()

    def accept(self):
        super(HideableQDialog, self).accept()
        self._finish_dialog()

    def exec_(self):
        status = super(HideableQDialog, self).exec_()
        self._mutex.lock()
        condition_succedeed = False
        while not condition_succedeed and not self._is_finished:
            condition_succedeed = self._finished_condition.wait(self._mutex, 10)
            QApplication.processEvents()
        self._mutex.unlock()
</code></pre>

python pyqt pyqt4