作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 example of Zetcode 开始学习 PySide并尝试编写具有两个窗口的应用程序:“布局 View ”的父级“示意图 View ”,每个窗口都有菜单栏。启动时应该只是原理图窗口,布局 win 应该通过菜单栏根目录中的 switchtoLAYOUT 启动。
我的问题是:
代码:
import sys
from PySide import QtCore, QtGui
class schematicWindow(QtGui.QMainWindow):
def __init__(self):
super(schematicWindow, self).__init__()
self.defineSchWin()
def defineSchWin(self):
exitAction = QtGui.QAction('&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
menubar.addMenu('&Edit')
menubar.addMenu('&Passives')
menubar.addMenu('&Descretes')
menubar.addMenu('&IC\'s')
swToLayMenu = menubar.addMenu('switchtoLAYOUT')
swToLayAction = QtGui.QAction(self)
swToLayAction.triggered.connect(self.layoutWindow)
swToLayMenu.addAction(swToLayAction) # open layoutWindow (if not exists)
# and set focus to layoutWindow
self.setGeometry(0, 300, 500, 300)
self.setWindowTitle('Schematic View')
self.show()
def layoutWindow(self):
window = QtGui.QMainWindow(self)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.statusBar()
menubar = window.menuBar()
switchtoSchMenu = menubar.addMenu('switchtoSCHEMATIC')
window.setGeometry(100, 600, 500, 300)
window.setWindowTitle('Layout View')
window.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = schematicWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
最佳答案
您需要在您的类中保留对布局窗口的引用(您应该将 self.layout_window = None
放在 __init__
中)。这个函数现在检查窗口是否已经初始化,如果还没有初始化,确保它可见,然后将新窗口设置为事件窗口。像这样的东西:(这没有经过测试)
def layoutWindow(self):
if self.layout_window is None:
window = QtGui.QMainWindow(self)
self.layout_window = window
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.statusBar()
menubar = window.menuBar()
switchtoSchMenu = menubar.addMenu('switchtoSCHEMATIC')
window.setGeometry(100, 600, 500, 300)
window.setWindowTitle('Layout View')
else:
window = self.layout_window
window.show()
window.activateWindow()
window.raise() # just to be sure it's on top
( doc )
关于qt - PySide 如何将焦点设置到新窗口并在不存在时将其打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14455536/
我是一名优秀的程序员,十分优秀!