简体   繁体   English

pyQt5 更改 MainWindow Flags

[英]pyQt5 change MainWindow Flags

I use python 3.4 , pyQt5 and Qt designer (Winpython distribution).我使用 python 3.4 、 pyQt5 和 Qt 设计器(Winpython 发行版)。 I like the idea of making guis by designer and importing them in python with setupUi.我喜欢由设计师制作 gui 并使用 setupUi 将它们导入 python 的想法。 I'm able to show MainWindows and QDialogs.我可以显示 MainWindows 和 QDialogs。 However, now I would like to set my MainWindow, always on top and with the close button only.但是,现在我想设置我的 MainWindow,始终在顶部并且仅使用关闭按钮。 I know this can be done by setting Windows flags.我知道这可以通过设置 Windows 标志来完成。 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_())

The MainWindow shows up (without error) but Flags are not applied. MainWindow 出现(没有错误)但未应用标志。 I suppose this is because I asked to change Windows properties after it was already created.我想这是因为我在创建后要求更改 Windows 属性。 Now, questions are : how I can do it without modify Ui_MainWindow directly?现在,问题是:如何在不直接修改 Ui_MainWindow 的情况下做到这一点? It is possible to change flags in Qt designer ?可以在 Qt 设计器中更改标志吗? Thanks谢谢

Every call of setWindowFlags will completely override the current settings, so you need to set all the flags at once.每次调用setWindowFlags都将完全覆盖当前设置,因此您需要一次设置所有标志。 Also, you must include the CustomizeWindowHint flag, otherwise all the other hints will be ignored.此外,您必须包含CustomizeWindowHint标志,否则所有其他提示都将被忽略。 The following will probably work on Windows:以下可能适用于 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: PS:

It is not possible to set the window flags in Qt Designer.无法在 Qt Designer 中设置窗口标志。

I would propose a different solution, because it keeps the existing flags.我会提出一个不同的解决方案,因为它保留了现有的标志。 Reason to do this, is to NOT mingle with UI-specific presets (like that a dialog has not by default a "maximize" or "minimize" button).这样做的原因是不要与特定于 UI 的预设混在一起(比如对话框默认没有“最大化”或“最小化”按钮)。

self.setWindowFlags(self.windowFlags() # reuse initial flags
    & ~QtCore.Qt.WindowContextHelpButtonHint # negate the flag you want to unset
)

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

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