How to access to ui members from the Designer in the QtCreator?
-
Hi, I'm just starting with QT Python and only want to access the members of the form.ui file. In the Qt description I find only infos regarding loading the form.ui file.
import sys import os from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import QFile from PySide6.QtUiTools import QUiLoader class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.load_ui() // how to access the QLineEdit element with the name Operant1??? def load_ui(self): loader = QUiLoader() path = os.path.join(os.path.dirname(__file__), "form.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() if __name__ == "__main__": app = QApplication([]) widget = MainWindow() widget.show() sys.exit(app.exec())
Thanks...
-
Hi and welcome to devnet,
This tutorial, while being written for PyQt, applies as well to PySide2/6.
You'll see there the various way to setup and use your designer based widgets.
Note that you will need to generate Python code out of your .ui files for that.
-
Thanks, I know this side already, but as I understand there is no description how to access members from a GUI which is directly loaded with the QUiLoader().
- Generate a file
$ pyuic5 -o main_window_ui.py ui/main_window.ui
and then access the members via
self.ui = Ui_MainWindow()
- Load the GUI via
loader = QUiLoader()
and have no access to the members, because there is no main_window_ui.py
What do I not correctly understand here?
-
@PowerNow
If you use thepyuic5
approach to "translate" the.ui
file into a.py
file --- which indeed I recommend --- you no longer useQUiLoader
. They are an either-or approach. Withpyuic5
you insteadimport main_window_ui.py
and then access the widgets etc. directly --- have a look at the code generated into that..._ui.py
file.and have no access to the members, because there is no main_window_ui.py
There is such a file --- it was generated when you ran
pyuic5 -o main_window_ui.py ui/main_window.ui
. -
Sorry for this confusion. Then I will ask again:
How can I access GUI members in python using the QUiLoader()?@JonB :
"and have no access to the members, because there is no main_window_ui.py"
I related this to "2.Load the GUI via QUiLoader()". -
@PowerNow
If you wish to useQUiLoader()
--- which I have never used --- with PySide2, why not Google for that with examples? There are so many hits. For example, one way is https://doc.qt.io/archives/qtforpython-5.12/PySide2/QtUiTools/QUiLoader.html#detailed-descriptiondef loadCustomWidget(parent): loader = QUiLoader() availableWidgets = loader.availableWidgets() if availableWidgets.contains("AnalogClock"): myWidget = loader.createWidget("AnalogClock", parent) return myWidget
or perhaps what you are looking for is https://stackoverflow.com/questions/20115693/correct-way-to-address-pyside-qt-widgets-from-a-ui-file-via-python
loader = QtUiTools.QUiLoader() ui = loader.load('filename.ui', parent) my_widget = ui.my_widget_name
Why not inspect what it is the returned
ui
in a Python debugger if you want to see what is going on there?In that stackoverflow post the second answer summarises well
There are two disadvantages of loading UI at run time:
and why that poster and I would recommend preferably the
uic
approach for development. But up to you. -
Thxs, thats it, I could not find that anywhere:
ui = loader.load('filename.ui', parent)
and missunderstand this in the Qt docu
window = loader.load(ui_file)
Yeah, you are right creating the .py is better than loading. But currently I'm trying both for prototyping and sometimes i forgot to create and then couldn't find the fault for ages.