問題描述
我使用 python 3.4、pyQt5 和 Qt 設(shè)計器(Winpython 發(fā)行版).我喜歡由設(shè)計師制作 gui 并使用 setupUi 將它們導(dǎo)入 python 的想法.我能夠顯示 MainWindows 和 QDialogs.但是,現(xiàn)在我想設(shè)置我的 MainWindow,總是在頂部并且只有關(guān)閉按鈕.我知道這可以通過設(shè)置 Windows 標(biāo)志來完成.我嘗試執(zhí)行以下操作:
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_())
顯示主窗口(沒有錯誤),但未應(yīng)用標(biāo)志.我想這是因為我在創(chuàng)建 Windows 屬性后要求更改它.現(xiàn)在,問題是:我如何在不直接修改 Ui_MainWindow 的情況下做到這一點?可以在 Qt 設(shè)計器中更改標(biāo)志嗎?謝謝
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
推薦答案
每次調(diào)用 setWindowFlags
都會完全覆蓋當(dāng)前設(shè)置,因此您需要一次設(shè)置所有標(biāo)志.此外,您必須包含 CustomizeWindowHint
標(biāo)志,否則所有其他提示將被忽略.以下可能適用于 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
)
但是,這不太可能適用于所有平臺.提示"確實就是這個意思.窗口管理器完全可以忽略這些標(biāo)志,并且不能保證它們都會以相同的方式運行.
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 中設(shè)置窗口標(biāo)志.
It is not possible to set the window flags in Qt Designer.
這篇關(guān)于pyQt5 更改 MainWindow 標(biāo)志的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!