PyQt4 : How can i toggle the “Stay On Top” behavior?

空扰寡人 提交于 2019-12-03 20:07:31

This should disable it:

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

This should enable it:

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

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

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!