問題描述
我使用 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.
這篇關于pyQt5 更改 MainWindow 標志的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!