基本上,对于最新版本的python中的pyside2 / pyqt,应用程序有启动器类型协议。在启动器中按下按钮后,我希望它关闭当前窗口类并打开下一个窗口(具有其自己的类)。在执行此操作时,我希望它将文件名传递给新窗口(基本上,告诉它要打开哪个文件)。这是我在努力的部分。以下代码段是我到目前为止的内容的片段,我认为对更好地帮助解释该问题很重要!
class launcherWidget( QMainWindow ) :
def __init__(self) :
#header
QMainWindow.__init__(self)
self.setWindowTitle("Scouting Software Launcher")
#main central table
self.table = QTableWidget(self)
self.table.setColumnCount(3)
self.table.setHorizontalHeaderLabels(["Save Name", "Mod Date","Creation Date"])
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table.verticalHeader().setVisible(False)
self.fill_table_with_list( listOfSavesLists )
self.table.resize(470,165)
self.table.move(15,50)
self.b4 = QPushButton("Open",self)
self.b4.clicked.connect(self.openSelected) #wip
self.b4.resize(70,25)
self.b4.move(340,225)
self.b5 = QPushButton("View",self)
self.b5.clicked.connect(self.viewSelected) #wip
self.b5.resize(70,25)
self.b5.move(420,225)
def viewSelected ( self ) :
print("placeholder") #THIS POINT TO CALL NEXT FUNCTION (I want to send a string)
def openSelected ( self ) :
print("placeholder")
@Slot()
def exit_app(self, checked):
QApplication.quit()
class viewWindow( QMainWindow ) :
def __init__( self , savename ) : #STRING TRANSFERED = savename
#header
QMainWindow.__init__(self)
self.setWindowTitle("Scouting Software Viewer")
self.openSaveName = str(savename) #deal with arg
@Slot()
def exit_app(self, checked) : #on exit, shutdown properly
QApplication.quit()
def onLaunch () :
app = QApplication(sys.argv)
window = launcherWidget()
window.resize(500,300)
window.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您可以创建一个带有字符串参数的信号,该信号将在您关闭主窗口时发出,或者在一种发出信号然后关闭窗口的方法中发出(这是我很可能选择自己的方式)。
关于将信号连接到哪个插槽,这完全由您决定,您可以使用全局级别的函数,或子类化QApplication并在其中添加方法,或者我确定还有其他方法。
from PySide2 import QtWidgets, QtCore
import sys
class launcherWidget(QtWidgets.QMainWindow):
launch_signal = QtCore.Signal(str)
def __init__(self):
# header
QtWidgets.QMainWindow.__init__(self)
self.setWindowTitle("Scouting Software Launcher")
# main central table
self.table = QtWidgets.QTableWidget(self)
self.table.setColumnCount(3)
self.table.setHorizontalHeaderLabels(["Save Name", "Mod Date", "Creation Date"])
self.table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.table.verticalHeader().setVisible(False)
self.table.setRowCount(1)
item = QtWidgets.QTableWidgetItem("Placeholder")
self.table.setItem(0, 0, item)
self.table.resize(470, 165)
self.table.move(15, 50)
self.b4 = QtWidgets.QPushButton("Open", self)
self.b4.clicked.connect(self.openSelected) # wip
self.b4.resize(70, 25)
self.b4.move(340, 225)
self.b5 = QtWidgets.QPushButton("View", self)
self.b5.clicked.connect(self.viewSelected) # wip
self.b5.resize(70, 25)
self.b5.move(420, 225)
def viewSelected(self):
self.launch_signal.emit('Placeholder')
self.close()
def openSelected(self):
print("placeholder")
def exit_app(self, checked):
QtWidgets.QApplication.quit()
class viewWindow(QtWidgets.QMainWindow):
def __init__(self, savename): # STRING TRANSFERED = savename
# header
QtWidgets.QMainWindow.__init__(self)
self.setWindowTitle("Scouting Software Viewer")
self.openSaveName = str(savename) # deal with arg
label = QtWidgets.QLabel(self.openSaveName)
self.setCentralWidget(label)
def exit_app(self, checked): # on exit, shutdown properly
QtWidgets.QApplication.quit()
def start_view_window(argument):
global view # Using a global now to avoid garbage collection, but other ways would be better
view = viewWindow(argument)
view.show()
def onLaunch():
app = QtWidgets.QApplication(sys.argv)
window = launcherWidget()
window.resize(500, 300)
window.launch_signal.connect(start_view_window)
window.show()
sys.exit(app.exec_())
onLaunch()