2013-01-22 30 views
2

我已经开始从example of Zetcode学习PySide并尝试代码应用程序,它有两个窗口打开:“图解视图”,这是“布局视图”父母,每个菜单酒吧。在开始时应该只是示意图窗口,并且布局win应该通过菜单栏的根目录switchtoLAYOUT启动。PySide如何将焦点设置到新的窗口,如果不存在

我的问题是:

  1. 如何使“switchtoLAYOUT”根不显示下拉列表,并仍然在做动作只有一个“布局视图”窗口的实例?
  2. 如何切换两个窗口(“switchtoLAYOUT”和“switchtoSCHEMATIC”)之间的重点是什么?
  3. 请检查我的代码,并建议我的东西智能(即不应该不难)。

验证码:

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() 

回答

4

你需要在你的类布局窗口的引用,(你应该把在__init__self.layout_window = None)。此功能现在检查,如果窗口已被初始化,使得它,如果它一直没有,确保它是可见的,然后设置新的窗口为活动窗口。是这样的:(这是未测试)

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

+1

责任。只需要更改为.raise_()并且它可以工作。你能帮助我如何点击“switchtoLAYOUT”而不必删除空的子菜单吗? – Alex

+0

我不明白你的问题。 – tacaswell

+0

如果我不清楚,请打扰一下。如果您单击“文件”它拉下来文件选项的子菜单,但“switchtoLAYOUT”没有选择,我想点击它立即做一个动作,不要拉下子菜单。 – Alex