Question

I'm making an application that when minimized go to tray icon, and the taskbar icon will be hidden.

Searching I found that I can hide the icon changing the windowflag to "ToolTip".

Then when I re-open the program I set again the "Window" flag. This works, but when I press the "X" to close the program it doesn't close, but it minimized to tray.

What could be the problem?

There is part of code:

Code of main window:

def changeEvent(self, e): 
    print e.type()
    if e.type() == e.WindowStateChange:
        if self.restore:
            print "Normalized"
            if platform.system() == "Windows":
                self.setWindowFlags(QtCore.Qt.Window)
            self.showNormal()
            self.activateWindow()
            restore = 0
            e.accept()
        elif self.windowState() & QtCore.Qt.WindowMinimized:
                print "Minimized"
                self.hide()
                    if platform.system() == "Windows":
                         self.setWindowFlags(QtCore.Qt.ToolTip)
                    event.accept()
        else:
                print "NONE"
def showW(self):
    self.restore = 1
    e = QtCore.QEvent(QtCore.QEvent.WindowStateChange)
    QtGui.QApplication.sendEvent(self,e)
    print e.isAccepted()

def closeEvent(self, event):
    reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    if reply == QtGui.QMessageBox.Yes:
        print "ByeBye"
        event.accept()
    else:
        event.ignore()

Code of Tray Icon:

def onTrayIconActivated(self, reason):
    if reason == QtGui.QSystemTrayIcon.DoubleClick:
        self.parent.showW()

All seem works fine if I never minimize the window, but after minimized I'm not able to close it.

I had the closerequest screen, but when I click Yes the program go to trayicon instead of close.

EDIT: If a comment the WindowFlag all works, then the problem is the restore of window flag? I'm using Python 2.7 and PyQt4.

Était-ce utile?

La solution

Seem to be solved!

I have added:

QtGui.QApplication.quit()

to closeEvent(), like this:

def closeEvent(self, event):
    reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    if reply == QtGui.QMessageBox.Yes:
        print "ByeBye"
        QtGui.QApplication.quit()
        event.accept()
    else:
        event.ignore()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top