accessing several .ui files in python
-
My objective is to have a main menu and several custom dialogs created in qt designer. Then through the main menu be able to invoke the custom dialogs in one python application. Is there a best practice for doing this? I would appreciate a simple python example. I was not able to have the main menu and the custom dialogs in one .ui file while loading both .ui files in the python program crashes the app.
-
@explorer100
You can have as many separate.ui
files as you want/need. Show your code which "crashes" on loading these. -
@JonB thank you Jon. I'm very new to qt designer... so first is it a good practice to have .ui files for each custom dialog then load each in the python app? or is it better (if at all possible) to have everything defined in one .ui file then load everything once?
-
@explorer100
On a separate note, especially since you are beginner. I would not take the approach of "loading".ui
files at runtime. Read through Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic. You sound like you are doing Option B: Loading it directly. If you do this you will not be able to access all your widgets so easily. I would always recommend the alternative, Option A: Generating a Python class. It's a touch more work --- you have to run apyside6-uic
tool step to produce Python code from the.ui
in a generated.py
file (I think Creator can run this for you each time) --- but in return you get proper coding-time support for all your widgets. -
OK.. thank you very much Jon... will use this approach!
-
Hello. Any recommended approach of communication between the main window and the custom dialogs?
-
@explorer100 said in accessing several .ui files in python:
Any recommended approach of communication between the main window and the custom dialogs?
As always in Qt: signals/slots
-
There are two basic possibilities:
- Expose "getter/setter" methods from the dialog which the outside world (main window) can call, for the dialog to set values in the widgets and return values after user interaction. This is usually the simplest approach, e.g. if all you do is
exec()
the dialog as modal. - For more advanced information exchange you could use signals/slots (in either/both directions). Depends what your custom dialogs do.
- Expose "getter/setter" methods from the dialog which the outside world (main window) can call, for the dialog to set values in the widgets and return values after user interaction. This is usually the simplest approach, e.g. if all you do is
-
This post is deleted!
-
@JonB said in accessing several .ui files in python:
You sound like you are doing Option B: Loading it directly. If you do this you will not be able to access all your widgets so easily. I would always recommend the alternative, Option A: Generating a Python class. It's a touch more work --- you have to run a pyside6-uic tool step to produce Python code from the .ui in a generated .py file (I think Creator can run this for you each time) --- but in return you get proper coding-time support for all your widgets.
An interesting thing I've noticed lately:
We had a similar topic in the German section where OP used theQUiLoader
/"direct" approach, but then struggled with (as you also mentioned) accessing and connecting the child- and sub-Widgets from the UI-basedQWidget
constructed byQUiLoader
.
I've also recommended using theuic
tool to convert the .ui to a .py class, esp. when you are a beginner.However, I was told that at some other place (Python forum) OP read about "the Python community" condemning this "generator" approach, since it violates some principles of interpreted languages... :D
Yeah, sure... :D
I'm not a Python-hater, or a Python-fanboy, but without already popular frameworks providing Python-bindings, nobody would use Python in these cases... the whole community profits. So you might have to accept it's downsides, which come with the fact that these frameworks weren't intented for Python-like languages and are only usable through bindings.Try to write your own platform independent, holistic GUI framework like Qt from scratch using Python only... Good luck with that ;-)