gpt4 book ai didi

qt - PySide 如何将焦点设置到新窗口并在不存在时将其打开

转载 作者:行者123 更新时间:2023-12-02 22:15:57 25 4
gpt4 key购买 nike

我从 example of Zetcode 开始学习 PySide并尝试编写具有两个窗口的应用程序:“布局 View ”的父级“示意图 View ”,每个窗口都有菜单栏。启动时应该只是原理图窗口,布局 win 应该通过菜单栏根目录中的 switchtoLAYOUT 启动。

我的问题是:

  1. 如何使根目录中的“switchtoLAYOUT”不显示下拉菜单并且仍然只对“布局 View ”窗口的一个实例执行操作?
  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()

最佳答案

您需要在您的类中保留对布局窗口的引用(您应该将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com