为什么当我将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()
最佳答案
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()
关于python - 切换QMainWindow WindowStaysOnTopHint标志pyside,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41622285/