I tried to to subclass QMainWindows and make a call to self.hide()
when the minimize event was caught. When I pressed the minimize
button, it is minimized to taskbar, and when I pressed it at the
taskbar so that it appear again on the screen, I found that it is not
invisible. Only the content of the QMainWindows is hide and the frame
is still there.
Is it what self.hide() supposed to behave?
I believe that's the correct behaviour. When you do self.hide()
you are not hiding the window (as suggested in the accepted answer of the linked question) but the widget in it. That's why when you do restore you see the frame, but not the widget.
In your linked question see the other answers and try them - there are code samples.
UPDATE:
Here is a working example from you linked question (tested in KDE with Python 2.7):
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout,QSystemTrayIcon
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.icon = QSystemTrayIcon()
r = self.icon.isSystemTrayAvailable()
print r
self.icon.setIcon(QtGui.QIcon(':/trolltech/styles/commonstyle/images/networkdrive-16.png'))
self.icon.show()
#self.icon.setVisible(True)
self.setGeometry(300, 300, 250, 150)
self.setWindowIcon(QtGui.QIcon(':/trolltech/styles/commonstyle/images/networkdrive-16.png'))
self.setWindowTitle('Message box')
self.show()
self.icon.activated.connect(self.activate)
self.show()
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?\n(choose No to minimize to tray, and double click to restore)",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
self.icon.show()
self.hide()
event.ignore()
def activate(self, reason):
print reason
if reason == 2:
self.show()
def __icon_activated(self, reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()