Note that there are some explanatory texts on larger screens.

plurals
  1. POPyQt: application unexpectedly leaves main loop
    primarykey
    data
    text
    <p>I have a simple PyQt application, which has a tray icon and can be hidden from taskbar by clicking close button or tray icon. Application can be closed from tray icon context menu. After user clicks "exit" in context menu, modal window with confirmation question appears . If user clicks "yes" application closes, if "no" application continues working.</p> <p>When main window is hidden, application will be closed, even if user click "No" in modal window, but everything correct when window isn't hidden. This also happens with any modal window, for example with some information. I suppose the are some "magic" in parent paarmeter for QtGui.QMessageBox.question, but I don't know how to handle it. Please help to fix this annoying bug.</p> <p>Here is the code:</p> <pre><code>import sys from datetime import datetime from PyQt4 import QtGui, QtCore class SampleWindow(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.init_ui() self.tray_icon.activated.connect(self.tray_click) self.show_window.triggered.connect(self.show_from_tray) self.now_button.triggered.connect(self.info) self.appexit.triggered.connect(self.app_close) def init_ui(self): self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Message box') self.tray_icon = QtGui.QSystemTrayIcon() self.tray_icon.setIcon(QtGui.QIcon('clock.ico')) self.tray_icon.show() self.iconMenu = QtGui.QMenu() self.show_window = self.iconMenu.addAction("MyApp") self.show_window.setDisabled(True) self.iconMenu.addSeparator() self.now_button = self.iconMenu.addAction("Now") self.appexit = self.iconMenu.addAction("Exit") self.tray_icon.setContextMenu(self.iconMenu) def info(self): now = str(datetime.now()) QtGui.QMessageBox.information(self, 'Now', now) def app_close(self): info_msg = "Are you sure to quit?" reply = QtGui.QMessageBox.question(self, 'Exit', info_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: QtGui.QApplication.quit() def closeEvent(self, event): self.hide_to_tray() event.ignore() def show_from_tray(self): self.setWindowFlags(QtCore.Qt.Window) self.showNormal() self.activateWindow() self.show_window.setDisabled(True) def hide_to_tray(self): self.setWindowFlags(QtCore.Qt.Tool) self.show_window.setDisabled(False) def tray_click(self, reason): if reason != QtGui.QSystemTrayIcon.Context: if self.isHidden(): self.show_from_tray() else: self.hide_to_tray() def main(): app = QtGui.QApplication(sys.argv) sample = SampleWindow() sample.show() sys.exit(app.exec_()) if __name__ == '__main__': main() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload