为什么当我将QMainWindow的SetWindowFlags切换到WindowStaysOnTopHint时,我的窗口会消失,更重要的是不能保持最佳状态?我正在使用PySide和QMainWindow。
import sys
from PySide import QtGui, QtCore
class Browser(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Browser, self).__init__(parent)
self.resize(200, 150)
self.setWindowTitle('Assets')
self.initUI()
def initUI(self):
self.mi_stay_on_top = QtGui.QAction('Stay On Top', self)
self.mi_stay_on_top.setShortcut('Ctrl+T')
self.mi_stay_on_top.setCheckable(True)
self.mi_stay_on_top.triggered.connect(self.toggle_stay_on_top)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(self.mi_stay_on_top)
grid = QtGui.QVBoxLayout()
grid.setContentsMargins(10,10,10,10)
self.setLayout(grid)
def toggle_stay_on_top(self):
if self.mi_stay_on_top.isChecked():
# enabled
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
else:
# disable
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
def main():
app = QtGui.QApplication(sys.argv)
ex = Browser()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 0 :(得分:2)
setWindowFlags
方法重置父级,隐藏窗口。
所以你需要这样做:
def toggle_stay_on_top(self):
if self.mi_stay_on_top.isChecked():
# enabled
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
else:
# disable
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
# re-show the window after changing flags
self.show()