pyQt5 更改 MainWindow 标志
问题描述
我使用 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.
相关文章