How to access a layout and layout's children after loading UI from QtDesigner?
-
Hey all.
I'm still a beginner when it comes to using Qt, and I've only just started using layouts and spacers to arrange my windows.
I use QtDesigner to build my ui and then connect my widgets to their functionalities with python.
Before using layouts, I could simply access any QWidget in the window by name as if it were a class property.
Now, since they're all nestled in layouts, I can't seem to find them. I tried accessing the layout as a class property and then using findChild on it, but findChild raised an exception (I thought it was supposed to receive (str, QObject) but it doesn't seem to like this input)What is the correct way of tackling this? How do I use findChild properly?
Thanks!
-
@Aya-Abeliovich
Hello and welcome.Using layouts should not affect this.
There are several ways of using UI-designer stuff from Python. Tell us whether you are using PySide2/PyQt5/PySide6/PyQt6. Also tell us just how you you "load" the designed UI at runtime: you might be generating
.py
file from.ui
file (sounds like you are not), you might be using someloadUi()
call on.ui
file at runtime, we need to see.IMHO the
loadUI()
-at-runtime and/or the need to usefindChild()
are not the "friendliest" way to choose to develop your code. I prefer the way that generates a.py
file for you from your.ui
file, a much nicer "experience" when coding. This does mean you have to run an extra step whenever you alter your.ui
file in Designer, but I think it's worth it. I don't know, but if you work inside Creator I think/hope that may be automated for you now, as it would be if you were using C++. -
Hey Jon,
thank you for your quick reply!
I am using PyQt5, and I really was using loadui. I haven't thought of converting my ui file to a .py file.
Now trying to use the converted file, I ran into another problem: first of all, it looks like it converted it to Python2 code. That's not too bad and can be easily fixed. My problem is that it's unclear how to use the generated class - it has no "init" function of its own.
Could you provide any insight on how to use it?
Couldn't find any proper explanations by Googling.Thanks a bunch.
Aya -
@Aya-Abeliovich said in How to access a layout and layout's children after loading UI from QtDesigner?:
first of all, it looks like it converted it to Python2 code. That's not too bad and can be easily fixed.
? Certainly not, Python 3 only!
Can't find PyQt5 Riverbank links any longer. Have a read of https://doc.bccnsoft.com/docs/PyQt5/designer.html#using-the-generated-code. There should be loads of examples out there somewhere.
-
Please take a look at Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic