Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pyside2: How can load new window inside Main Window?
Qt 6.11 is out! See what's new in the release blog

Pyside2: How can load new window inside Main Window?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MarconeM Offline
    MarconeM Offline
    Marcone
    wrote on last edited by
    #1

    Example: I have two files: main.py and new.py. I want that when I click a button on main.py it loads new.py without being in a new window. And when I click on a button inside new.py it goes back to main.py. Could someone give me a simple code example of how to do this?

    JonBJ 1 Reply Last reply
    0
    • MarconeM Marcone

      Example: I have two files: main.py and new.py. I want that when I click a button on main.py it loads new.py without being in a new window. And when I click on a button inside new.py it goes back to main.py. Could someone give me a simple code example of how to do this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Marcone
      Depends what main.py & new.py` have in them, what widgets they define and how, etc.

      It is often the case with this kind of question that you should consider using a QStackedWidget. But as I say, depends what you have.

      MarconeM 1 Reply Last reply
      0
      • JonBJ JonB

        @Marcone
        Depends what main.py & new.py` have in them, what widgets they define and how, etc.

        It is often the case with this kind of question that you should consider using a QStackedWidget. But as I say, depends what you have.

        MarconeM Offline
        MarconeM Offline
        Marcone
        wrote on last edited by
        #3

        @JonB said in Pyside2: How can load new window inside Main Window?:

        main.py & new.py`

        main.py and new.py are independent MainWindows form. Not QStackedWidget case.

        JonBJ 1 Reply Last reply
        0
        • MarconeM Marcone

          @JonB said in Pyside2: How can load new window inside Main Window?:

          main.py & new.py`

          main.py and new.py are independent MainWindows form. Not QStackedWidget case.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Marcone
          Most likely the wrong way to do things. And still likely that you should be using QStackedWidget.

          Then if they are both of class MainWindow, what does your

          it loads new.py without being in a new window

          mean?

          MarconeM 1 Reply Last reply
          0
          • JonBJ JonB

            @Marcone
            Most likely the wrong way to do things. And still likely that you should be using QStackedWidget.

            Then if they are both of class MainWindow, what does your

            it loads new.py without being in a new window

            mean?

            MarconeM Offline
            MarconeM Offline
            Marcone
            wrote on last edited by Marcone
            #5

            @JonB I want create every window in separated .ui forms and call everyone when needed. How can I make it? can you send simple example?

            JonBJ 1 Reply Last reply
            0
            • MarconeM Marcone

              @JonB I want create every window in separated .ui forms and call everyone when needed. How can I make it? can you send simple example?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Marcone
              You can paste your code if you wish, not send me something. I am not going to send you anything.

              I have no idea what you mean by having separated forms, and each of type QMainWindow, but displaying each one without it being a new window. Showing code won't explain that to me.

              Since you won't consider QStackedWidget, at the end of the day you will presumably want pushbutton code which just show()s the other window and hide()s the current window, and that's all there is to say.

              MarconeM 1 Reply Last reply
              0
              • JonBJ JonB

                @Marcone
                You can paste your code if you wish, not send me something. I am not going to send you anything.

                I have no idea what you mean by having separated forms, and each of type QMainWindow, but displaying each one without it being a new window. Showing code won't explain that to me.

                Since you won't consider QStackedWidget, at the end of the day you will presumably want pushbutton code which just show()s the other window and hide()s the current window, and that's all there is to say.

                MarconeM Offline
                MarconeM Offline
                Marcone
                wrote on last edited by Marcone
                #7

                My code: This file is called Program.py and import two files: untitled.py (Main) and newform.py (New window to load)

                # -*- coding: utf-8 -*- 
                import sys
                from PySide2 import QtCore, QtGui, QtWidgets
                from untitled import Ui_MainWindow
                from newform import Ui_Form
                
                
                class Form2(QtWidgets.QMainWindow, Ui_Form):
                    def __init__(self):
                        super(Form2, self).__init__()
                        self.setupUi(self)
                        self.bt2.clicked.connect(self.fun)
                
                        def fun(self):
                            print('Go to form 1')
                
                class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
                    def __init__(self):
                        super(MainWindow, self).__init__()
                        self.setupUi(self)
                        self.bt1.clicked.connect(self.fun)
                        
                    def fun(self):
                        print('Go to form 2')
                        
                if __name__ == "__main__":
                    app = QtWidgets.QApplication(sys.argv)
                    window = MainWindow()
                    window.show()
                    sys.exit(app.exec_())
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved