0

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 Answers2

0

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()
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • The hideEvent and changeEvent did not trigger when `Show Desktop` was clicked. Maybe window does not get minimized when `Show Desktop` is clicked. Also I tried this in **Manjaro OS** and the `hideEvent` got triggered but the widget was still minimized! – funky_poseidon Nov 10 '20 at 16:58
  • I tried with an old windows version, so it's possible that the event doesn't get triggered on new implementations (even if it should, according to documentation). On Linux it might depend on how the window manager behaves, as not all respect X11 standards. If you add a simple print statement in the `if`, does it get printed? If it doesn't, the only alternative I could think of is to do your own debugging by printing *all* events in changeEvent and try to get what events get triggered: use `print(event, event.type())` and compare it with the [docs](https://doc.qt.io/qt-5/qevent.html#Type-enum). – musicamante Nov 10 '20 at 18:01
  • Yeah I tried `print(event,event.type())` and found that no event was triggered when clicked on Show Desktop. – funky_poseidon Nov 10 '20 at 19:39
  • Thanks for the help I finally found the answer and I posted it. – funky_poseidon Nov 10 '20 at 20:06
0

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