python - PyQT4: why does QDialog returns from exec_() when setVisible(false) -
i'm using python 2.7 , pyqt4.
i want hide modal qdialog instance , later on show again. however, when dialog.setvisible(false) called (e.g., using qtimer), dialog.exec_() call returns (with qdialog.rejected return value).
however, according http://pyqt.sourceforge.net/docs/pyqt4/qdialog.html#exec, _exec() call should block until user closes dialog.
is there way hide dialog without _exec() returning?
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os 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 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_())
ps1: code prints following output:
hide event dialog exited status 0 :: 1 0
(the close event not called).
ps2: context, i'm trying implement systemtrayicon allows hide , restore qmainwindow (this part fine) , possibly modal qdialog without closing dialog.
thanks!
you can bypass normal behaviour of qdialog.setvisible
(which implicitly closes dialog), calling base-class method instead:
def hide_dialog(self): # self.dialog.setvisible(false) qtgui.qwidget.setvisible(self.dialog, false)
however, might preferrable connect dialog's finished()
signal, rather using exec()
, , explicitly reject()
dialog in closeevent
.
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os pyqt4 import qtgui, qtcore class mydialog(qtgui.qdialog): def __init__(self, parent): qtgui.qdialog.__init__(self, parent) layout = qtgui.qhboxlayout(self) title, slot in ('ok', self.accept), ('cancel', self.reject): button = qtgui.qpushbutton(title) button.clicked.connect(slot) layout.addwidget(button) def closeevent(self, qcloseevent): print "close event" self.reject() 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.finished.connect(self.dialog_finished) self.dialog.setmodal(true) self.dialog.show() qtcore.qtimer.singleshot(3000, self.dialog.hide) def dialog_finished(self, status): print "dialog exited status:", status if __name__ == '__main__': app = qtgui.qapplication([]) w = mywindow() w.show() sys.exit(app.exec_())
Comments
Post a Comment