简体   繁体   English

PyQt4:我如何切换“保持最佳状态”行为?

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

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. 在PyQt4中,很容易创建一个始终位于顶部的窗口。 This is covered here : PyQt: Always on top 这里包括: PyQt:始终在最前面

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. 更新在İsmail'cartman'Dönmez的建议之后,我搜索了一下,我在PyQt4中找到了WindowFlags示例的实现。

It can be found here 它可以在这里找到

This should disable it: 这应该禁用它:

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

This should enable it: 这应该使它:

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

Rosh is correct. 罗什是对的。 But don't forget to include window.show() after you change the flag. 但是在更改标志后不要忘记包含window.show()。 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) window.setWindowFlags(window.windowFlags()&~QtCore.Qt.WindowStaysOnTopHint)

This will enable it: 这将使它:

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

This will toggle it: 这将切换它:

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

您需要Qt::WindowStaysOnTopHint提示,请参阅Window Flags示例

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. 我无法评论Rosh的答案 ,所以请注意这里 - 对于Devuan的PyQt 5.7,如果你在QDialog上显示后切换WindowStaysOnTopHint标志,窗口就会消失,你需要在之后立即再次显示()。 Other than this it works. 除此之外它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM