self.raise_()
后跟 aself.activateWindow()
应该是您要查找的命令,尽管在我的 Debian 操作系统上似乎存在某种问题,例如,如果我单击最大化的窗口,该窗口将获得焦点,但它会也消失了,看起来像某种错误,setTopLevelWindow
方法中的序列将绕过该行为:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork
class myWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.button = QtGui.QPushButton(self)
self.button.setText("Show Dialog")
self.dialog = QtGui.QDialog(self)
self.dialog.setFocusPolicy(QtCore.Qt.StrongFocus)
self.dialog.installEventFilter(self)
self.button.clicked.connect(self.dialog.show)
self.setCentralWidget(self.button)
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.WindowDeactivate:
self.setTopLevelWindow()
self.dialog.close()
return True
return False
def setTopLevelWindow(self):
if self.windowState() != QtCore.Qt.WindowMaximized:
self.showMaximized()
self.showNormal()
else:
self.showNormal()
self.showMaximized()
self.raise_()
self.activateWindow()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())