PyQt4 : How can i toggle the "Stay On Top" behavior?
Asked Answered
C

4

6

I want to create an app, where the user will decide it the main window will stay always on top of the other apps.

In PyQt4 it is easy to create a window that will stay always on top. This is covered here : PyQt: Always on top

What I want to have a widget (menu item, checkbox etc) that will toggle this behavior on or off. So far i haven't found a way to reset the original behavior.

thank you

UPDATE After the suggestion of İsmail 'cartman' Dönmez, I searched a bit more and I found an implementation of the WindowFlags example in PyQt4.

It can be found here

Cindacindee answered 31/1, 2011 at 11:58 Comment(0)
C
17

This should disable it:

window.setWindowFlags(window.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)

This should enable it:

window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
Collaboration answered 31/1, 2011 at 12:18 Comment(2)
It took me a while to find that one should call windowFlags() to preserve Qt.WindowFlags type. If I try to use XOR and build up flags from scratch (in my case Qt.Dialog ^ Qt.WindowContextHelpButtonHint), I get TypeError as I end up with int instead of Qt.WindowFlags.Vouvray
This should be the accepted answer. It shows way to disable/enable thisAhmedahmedabad
B
5

Rosh is correct. But don't forget to include window.show() after you change the flag. Your window will be hidden when the flag is changed. I have also included the code for toggling the flag.

This will clear it:

window.setWindowFlags(window.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)

This will enable it:

window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)

This will toggle it:

window.setWindowFlags(window.windowFlags() ^ QtCore.Qt.WindowStaysOnTopHint)

Boredom answered 8/10, 2015 at 20:57 Comment(0)
N
4

You want the Qt::WindowStaysOnTopHint hint, see Window Flags Example.

Niggardly answered 31/1, 2011 at 12:3 Comment(1)
Thank you for your answer. Actually the PyQt version of the example can be found here : blindvic.livejournal.com/9437.htmlCindacindee
K
2

I'm not able to comment on Rosh's answer, so noting here - for PyQt 5.7 on Devuan, if you toggle the WindowStaysOnTopHint flag on a QDialog after it is shown, the window disappears and you need to show() it again immediately after. Other than this it works.

Kinetic answered 5/11, 2017 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.