1
import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        style = self.style()

        # Set the window and tray icon to something
        icon = style.standardIcon(QtGui.QStyle.SP_MediaSeekForward)
        self.tray_icon = QtGui.QSystemTrayIcon()
        self.tray_icon.setIcon(QtGui.QIcon(icon))
        self.setWindowIcon(QtGui.QIcon(icon))

        # Restore the window when the tray icon is double clicked.
        self.tray_icon.activated.connect(self.restore_window)

        # why this doesn't work?
        self.hide()
        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.Tool)

    def event(self, event):
        if (event.type() == QtCore.QEvent.WindowStateChange and 
                self.isMinimized()):
            # The window is already minimized at this point.  AFAIK,
            # there is no hook stop a minimize event. Instead,
            # removing the Qt.Tool flag should remove the window
            # from the taskbar.
            self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.Tool)
            self.tray_icon.show()
            return True
        else:
            return super(Example, self).event(event)

    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:
            event.accept()
        else:
            self.tray_icon.show()
            self.hide()
            event.ignore()

    def restore_window(self, reason):
        if reason == QtGui.QSystemTrayIcon.DoubleClick:
            self.tray_icon.hide()
            # self.showNormal will restore the window even if it was
            # minimized.
            self.showNormal()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

I want to make it only show the tray icon on startup, the example is from here:

I added the following to initUI(), but still it shows a blank window on startup. If I minimize the window, it disppears, leaving only the tray icon - but how can I make this happen on startup automatically?

  self.hide()
  self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.Tool)

edit: I tried this code on two different machines with Fedora 13 , and CentOS 7

Community
  • 1
  • 1
Shuman
  • 3,914
  • 8
  • 42
  • 65
  • I already stated in my question that I got this example from that link you posted. That answer doesn't give me the result I am looking for. I tried every answer below it, that's why I asked the question here. – Shuman Mar 02 '16 at 00:12
  • Fair enough. I've made the linked question more obvious. Maybe you should also state what platform you're on. – ekhumoro Mar 02 '16 at 02:00

2 Answers2

1

The solution is to show the tray-icon, but hide the window. Your example works for me on Windows if I make the following changes:

    def initUI(self):
        ...
        self.hide()
        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.Tool)
        # explicitly show the tray-icon
        self.tray_icon.show()    


def main():
    ...
    ex = Example()
    # don't re-show the window!
    # ex.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

QWidget::hide() should do what you want. The problem is you are calling show() on the new instance which is undoing the call to self.hide() in the constructor.

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()    # Remove this line
    sys.exit(app.exec_())
user3419537
  • 4,740
  • 2
  • 24
  • 42
  • just tried, this doesn't work. the window did not show, but also the tray icon is not showing. and hence there is no way to restore the window. – Shuman Mar 01 '16 at 23:15