Search code examples
pythonpyqt5taskbar

Hide the app from screen but not from taskbar


I would like to hide the app from screen but not from taskbar, I tried this:

app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
w.show()
w.resize(0, 0)

but it doesn't work, any idea?


Solution

  • I use QMainWindow instead of QWidget, then I override the focusInEvent and focusOutEvent events.

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    
    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5.QtCore import Qt
    from sys import argv, exit
    
    class Window(QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.setFocusPolicy(Qt.StrongFocus)
    
        def focusInEvent(self, event):
            print('focusInEvent')
            self.setWindowTitle('focusInEvent')
            self.showMinimized()
    
        def focusOutEvent(self, event):
            print('focusOutEvent')
            self.setWindowTitle('focusOutEvent')
    #        self.showMinimized()
    
    if __name__ == '__main__':
        app = QApplication([])
        w = Window()
        w.showMinimized()
        exit(app.exec_())