How to make pyqt app stay on bottom of all windows like Rainmeter?

356 Views Asked by At

I need to have my window stay on bottom. I tried using WindowStaysOnBottomHint but when Win+D or Show Desktop is clicked the app minimizes.

I researched and found that Rainmeter reorders the Z-index when Show Desktop is clicked using Win32 Api SetWindowPos but I am unable to find a solution for python QT.

Please give me solution!!

2

There are 2 best solutions below

0
funky_poseidon On BEST ANSWER

I found the solution using win32 API for python (Only for windows) . Refer SetWindowPos, SetWindowLong, win32gui

if sys.platform=='win32':
    from ctypes import windll
    import win32gui,win32con
    win32gui.SetWindowPos(window.winId(),win32con.HWND_BOTTOM, 0,0,0,0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE  | win32con.SWP_NOACTIVATE )

    hwnd=win32gui.GetWindow(win32gui.GetWindow(windll.user32.GetTopWindow(0),win32con.GW_HWNDLAST),win32con.GW_CHILD);
    win32gui.SetWindowLong(window.winId(),win32con.GWL_HWNDPARENT,hwnd)

window is the QTWidget window

4
musicamante On

A simple workaround is to check for the hideEvent() and ensure that the event is spontaneous (meaning that the event was originated from outside the application, like from the system in your case), and then call showNormal():

class MyWindow(QtWidgets.QWidget):
    # ...
    def hideEvent(self, event):
        if event.spontaneous():
            self.showNormal()

Just to be safe, you can also check for changeEvent() and filter for WindowStateChange events:

    def changeEvent(self, event):
        if (event.type() == QtCore.QEvent.WindowStateChange and 
            event.spontaneous() and 
            self.windowState() == QtCore.Qt.WindowMinimized):
                self.showNormal()