问题描述
我使用 python 3.4、pyqt5 和 qt 设计器(winpython 发行版).我喜欢由设计师制作 gui 并使用 setupui 将它们导入 python 的想法.我能够显示 mainwindows 和 qdialogs.但是,现在我想设置我的 mainwindow,总是在顶部并且只有关闭按钮.我知道这可以通过设置 windows 标志来完成.我尝试执行以下操作:
i use python 3.4 , pyqt5 and qt designer (winpython distribution). i like the idea of making guis by designer and importing them in python with setupui. i'm able to show mainwindows and qdialogs. however, now i would like to set my mainwindow, always on top and with the close button only. i know this can be done by setting windows flags. i tried to do as following:
from pyqt5 import qtcore, qtgui, qtwidgets import sys class mainwindow(qtwidgets.qmainwindow,ui_mainwindow): def __init__(self, parent=none): super(mainwindow, self).__init__(parent) self.setupui(self) self.setwindowflags(qtcore.qt.windowclosebuttonhint | qtcore.qt.windowminimizebuttonhint) self.setwindowflags(qtcore.qt.windowstaysontophint) if __name__ == '__main__': app = qtwidgets.qapplication(sys.argv) form = mainwindow() form.show() sys.exit(app.exec_())
显示主窗口(没有错误),但未应用标志.我想这是因为我在创建 windows 属性后要求更改它.现在,问题是:我如何在不直接修改 ui_mainwindow 的情况下做到这一点?可以在 qt 设计器中更改标志吗?谢谢
the mainwindow shows up (without error) but flags are not applied. i suppose this is because i asked to change windows properties after it was already created. now, questions are : how i can do it without modify ui_mainwindow directly? it is possible to change flags in qt designer ? thanks
推荐答案
每次调用 setwindowflags 都会完全覆盖当前设置,因此您需要一次设置所有标志.此外,您必须包含 customizewindowhint 标志,否则所有其他提示将被忽略.以下可能适用于 windows:
every call of setwindowflags will completely override the current settings, so you need to set all the flags at once. also, you must include the customizewindowhint flag, otherwise all the other hints will be ignored. the following will probably work on windows:
self.setwindowflags( qtcore.qt.window | qtcore.qt.customizewindowhint | qtcore.qt.windowtitlehint | qtcore.qt.windowclosebuttonhint | qtcore.qt.windowstaysontophint )
但是,这不太可能适用于所有平台.提示"确实就是这个意思.窗口管理器完全可以忽略这些标志,并且不能保证它们都会以相同的方式运行.
however, it is highly unlikely this will work on all platforms. "hint" really does mean just that. window managers are completely free to ignore these flags and there's no guarantee they will all behave in the same way.
ps:
不能在 qt designer 中设置窗口标志.
it is not possible to set the window flags in qt designer.