pyQt5更改MainWindow标志 [英] pyQt5 change MainWindow Flags

查看:663
本文介绍了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之后,我要求更改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.

这篇关于pyQt5更改MainWindow标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆