0

Basically I have a launcher type deal for an application goin on with pyside2 / pyqt in the latest version of python. Upon a button press in the launcher I want it to close the current window class and open the next window(which has its own class). While doing this I would like it to pass a filename to the new window(basically, tell it which file to open). This is the part that I'm struggling with. The following code snippets are a snippet of what I have so far that I felt was important to better help explain the question!

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


pppery
  • 3,731
  • 22
  • 33
  • 46
AweBob
  • 23
  • 1
  • 4

1 Answers1

0

You could create a signal with a string argument, which would get emitted either on close of your main window, or in a method that emits the signal then closes the window (which is the way I would most likely choose myself).

As to which slot to connect that signal to, it's sort of up to you, you could have a global level function, or subclass QApplication and add a method there, or I'm sure there are other ways.

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()
Erwan Leroy
  • 160
  • 10
  • Hmmmm, trying to wrap my head around what is going on here, but it works! And to that, I cannot complain!!! I get all of it except for the QTCore.Signal, but it's a good thing I can google that! Thank You very much! – AweBob Sep 22 '19 at 22:22
  • Signals are similar to the button.clicked that you used in your example. clicked is a signal on QPushButton. Here I just defined a custom one. See https://wiki.qt.io/Qt_for_Python_Signals_and_Slots – Erwan Leroy Sep 22 '19 at 22:33