Loading multiple .ui files
-
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()) -
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())@SamuraiDestroy said in Loading multiple .ui files:
perlinWindow = uiLoader.load(perlin)
perlInWindowis a Python local variable to themenuTriggers()function. As soon as that exits --- after theshow()and theprint()--- 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
perlInWindowbe Pythonglobal. If you were inside a class you would probably store it inself, 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
.uifile dynamic loading. Before you get into the habit of using it you would be much better off changing over to theuicmethod. 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. -
@SamuraiDestroy said in Loading multiple .ui files:
perlinWindow = uiLoader.load(perlin)
perlInWindowis a Python local variable to themenuTriggers()function. As soon as that exits --- after theshow()and theprint()--- 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
perlInWindowbe Pythonglobal. If you were inside a class you would probably store it inself, 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
.uifile dynamic loading. Before you get into the habit of using it you would be much better off changing over to theuicmethod. 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.@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?
-
S SamuraiDestroy has marked this topic as solved