dutt
dutt

Reputation: 8209

Hide window from taskbar

I'm trying to minimize a window to the tray, but it seems it refuses to hide from the taskbar. I've spent a little time and distilled the problem code down to this. It's not mcuh so I'm wondering if I need something else to hide my app to tray in Windows 7.

import sys, os
from PyQt4 import uic 
from PyQt4.QtGui import QMainWindow, QApplication

class MyClass(QMainWindow):
    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)
        self.ui = uic.loadUi(os.path.join("gui", "timeTrackerClientGUI.ui"), self)
    def hideEvent(self, event):
        self.hide()
    def showEvent(self, event):
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wnd = MyClass()
    wnd.show()
    app.exec_()

It seems the application icon does hide but then another one pops up, If I click the taskbar icon multiple times I can get these two icons flickering, looks kind of like this for a splitsecond before the first one hides:

alt text

Upvotes: 2

Views: 3819

Answers (2)

dutt
dutt

Reputation: 8209

It took a quite ugly hack to get it working but here's the final code if anybody is interested, ph is my platform-specific module, you can use platform.name or similar function instead:

def hideEvent(self, event):
    self.hide()
    if ph.is_windows():
        self.hidden = True
        self.setWindowFlags(Qt.ToolTip)
def showEvent(self, event):
    if ph.is_windows() and self.hidden:
        self.setWindowFlags(Qt.Window)
        self.hidden = False
    self.show()

Upvotes: 3

Frank Osterfeld
Frank Osterfeld

Reputation: 25155

calling show/hide in showEvent()/hideEvent() doesn't make sense - those events are the result of show()/hide() calls (and the like), not the trigger. If you want to toggle the window visiblity by clicking the tray icon, try setVisible(!isVisible()) on the widget, if you want to hide the window when the user clicks the window close button try reimplementing closeEvent():

MyMainWindow::closeEvent( QCloseEvent* e ) {
    hide();
    e->accept();
}

In Python, that is

def closeEvent(self, event):
    self.hide()
    event.accept()

Upvotes: 1

Related Questions