Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Loading multiple .ui files
Qt 6.11 is out! See what's new in the release blog

Loading multiple .ui files

Scheduled Pinned Locked Moved Solved Qt for Python
pysidepython
3 Posts 2 Posters 85 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.
  • S Offline
    S Offline
    SamuraiDestroy
    wrote last edited by
    #1

    I want the user to be able to click on a certain button to open a new window from a different .ui file to the main window, but my current code to do so doesn't do anything. My main window loads perfectly and the button input is correctly detected, it just seems that only the first .ui file my code loads actually opens. Here is my python code:

    import sys
    import os
    from PySide6 import QtCore, QtWidgets, QtGui
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtCore import QFile
    import screeninfo
    
    uiPath = os.path.dirname(os.path.abspath(__file__)) + "/ui/"
    # All ui files are stored in this path
    
    monitor = screeninfo.get_monitors()[0]
    x = monitor.width
    y = monitor.height
    uiLoader = QUiLoader()
    mainWindow = QFile(uiPath + "mainwindow.ui")
    perlin = QFile(uiPath + "perlinnoise.ui")
    # Fetch things necessary for UI loading
    
    def menuTriggers(button):
        print("you presse d button!!", button.objectName())
        if button.objectName() == "actionPerlin_Noise":
            perlinWindow = uiLoader.load(perlin)
            perlinWindow.show()
            print("it must be visible noww") # this does print so my the button is definitely detected
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = uiLoader.load(mainWindow)
        window.show()
        window.menubar.triggered.connect(menuTriggers)
        sys.exit(app.exec())
    
    
    JonBJ 1 Reply Last reply
    0
    • S SamuraiDestroy

      I want the user to be able to click on a certain button to open a new window from a different .ui file to the main window, but my current code to do so doesn't do anything. My main window loads perfectly and the button input is correctly detected, it just seems that only the first .ui file my code loads actually opens. Here is my python code:

      import sys
      import os
      from PySide6 import QtCore, QtWidgets, QtGui
      from PySide6.QtUiTools import QUiLoader
      from PySide6.QtCore import QFile
      import screeninfo
      
      uiPath = os.path.dirname(os.path.abspath(__file__)) + "/ui/"
      # All ui files are stored in this path
      
      monitor = screeninfo.get_monitors()[0]
      x = monitor.width
      y = monitor.height
      uiLoader = QUiLoader()
      mainWindow = QFile(uiPath + "mainwindow.ui")
      perlin = QFile(uiPath + "perlinnoise.ui")
      # Fetch things necessary for UI loading
      
      def menuTriggers(button):
          print("you presse d button!!", button.objectName())
          if button.objectName() == "actionPerlin_Noise":
              perlinWindow = uiLoader.load(perlin)
              perlinWindow.show()
              print("it must be visible noww") # this does print so my the button is definitely detected
      
      if __name__ == "__main__":
          app = QtWidgets.QApplication(sys.argv)
          window = uiLoader.load(mainWindow)
          window.show()
          window.menubar.triggered.connect(menuTriggers)
          sys.exit(app.exec())
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by JonB
      #2

      @SamuraiDestroy said in Loading multiple .ui files:

      perlinWindow = uiLoader.load(perlin)

      perlInWindow is a Python local variable to the menuTriggers() function. As soon as that exits --- after the show() and the print() --- it is released/destroyed and your window goes with it. You wont even see it flash up and disappear. In Python you have to be careful about scope/lifetime of variable references to objects, and make them persist.

      From where you are now you could probably make perlInWindow be Python global. If you were inside a class you would probably store it in self, but you are not working with your own classes.

      On a side note: as per another newcomer's question in another topic, I think you should re-examine your use of .ui file dynamic loading. Before you get into the habit of using it you would be much better off changing over to the uic method. See @friedemannkleint reply to a related situation at https://forum.qt.io/post/839262 and my reply. You/nearly everybody should be using Option A: Generating a Python class where you are currently using Option B: Loading it directly.

      S 1 Reply Last reply
      2
      • JonBJ JonB

        @SamuraiDestroy said in Loading multiple .ui files:

        perlinWindow = uiLoader.load(perlin)

        perlInWindow is a Python local variable to the menuTriggers() function. As soon as that exits --- after the show() and the print() --- it is released/destroyed and your window goes with it. You wont even see it flash up and disappear. In Python you have to be careful about scope/lifetime of variable references to objects, and make them persist.

        From where you are now you could probably make perlInWindow be Python global. If you were inside a class you would probably store it in self, but you are not working with your own classes.

        On a side note: as per another newcomer's question in another topic, I think you should re-examine your use of .ui file dynamic loading. Before you get into the habit of using it you would be much better off changing over to the uic method. See @friedemannkleint reply to a related situation at https://forum.qt.io/post/839262 and my reply. You/nearly everybody should be using Option A: Generating a Python class where you are currently using Option B: Loading it directly.

        S Offline
        S Offline
        SamuraiDestroy
        wrote last edited by
        #3

        @JonB said in Loading multiple .ui files:

        On a side note: as per another newcomer's question in another topic, I think you should re-examine your use of .ui file dynamic loading. Before you get into the habit of using it you would be much better off changing over to the uic method. See @friedemannkleint reply to a related situation at https://forum.qt.io/post/839262 and my reply. You/nearly everybody should be using Option A: Generating a Python class where you are currently using Option B: Loading it directly.

        What exactly makes Option A better than Option B?

        1 Reply Last reply
        0
        • S SamuraiDestroy has marked this topic as solved

        • Login

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