-1

I have developed an application using pyQt6, and when I run it, I would like to first open a QDialog, which should contains a "start" button and do some required pre processing, and then launch the whole application. To launch the "whole application", I use the following code :

class Launcher:

    def __init__(self):

        super().__init__()
        # OPEN A DIALOG HERE
        # ON CLICK OF BUTTON
        # CLOSE DIALOG, CALL CONTROLLER
        self.controller = Controller()


class Controller:


    def __init__(self):

        self.view= View()


class View(QMainWindow):


    def __init__(self, controller):

        super().__init__()
        self.ui = ui()
        self.ui.setupUi(self)
        self.show()

What I want is the Launcher class to open a dialog before doing a self.controller = Controller(), and only then open the main window by doing the call (as described in the commented part of my code).

I am not able to find a way to do it nicely, and more importantly in a way that makes sense for the code. If I need to do that, it's because depending on the user's click, the main window that will be open is not the same.

So far I've tried to use this launcher class :

class Launcher(QDialog):

    def __init__(self):

        super().__init__()

        self.filters = " All Files (*.*);;"

        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.show()
        self.ui.pushButton.clicked.connect(self.open_document)
        self.controller = None


    def open_document(self):

        filename, _ = QFileDialog.getOpenFileName(self, filter=self.filters)

        file = Path(filename)
        self.launch(file)


    def launch(self, file):

        if condition1:
            self.controller = Controller1(file)

        elif condition2:
            self.controller = Controller2(file)

        # self.close()



if __name__ == '__main__':

    # app = QApplication(sys.argv)
    launcher = Launcher()
    # sys.exit(app.exec())

Thanks in advance,

Regards.

PS. I've added the pyqt5 tag as it is more popular and very similar to pyqt6, so solution could be the same and would be more than welcome anyway.

  • If you want to use a MVC pattern, you should use it consistently: making the launcher responsible of the creation of the controller seems a further and unnecessary complication; instead, the controller (being a *controller*) should be responsible of "launching the launcher", and then eventually call the main window depending on its result. – musicamante Aug 21 '23 at 17:28
  • Thanks @musicamante, you're completely right, I was stuck in a way of thinking for absolutely no reason. – Suyop Ceat Aug 22 '23 at 08:00

0 Answers0