我从example of Zetcode开始学习PySide并尝试编写具有两个窗口的应用程序:“原理图 View ”,它是“布局 View ”的父级,每个窗口都有菜单栏。启动时,它应该只是原理图窗口,布局 win 应该通过菜单栏根目录中的 switchtoLAYOUT 启动。
我的问题是:
- 如何使根目录中的“switchtoLAYOUT”不显示下拉列表,并且仍然仅对“布局 View ”窗口的一个实例执行操作?
- 如何在两个窗口之间切换焦点(“switchtoLAYOUT”和“switchtoSCHEMATIC”)?
- 请检查我的代码并向我提出一些明智的建议(这应该不难)。
代码:
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/