How to connect UI to code
-
wrote on 23 May 2022, 19:45 last edited by
I have designed a basic form with several items on it; one of which is called dumpText, which is a Text Edit widget (QTextEdit).
I have started writing a little code and want to assign some data to the textbox, dumpText.
However, all the examples on the web and in the book have all the objects on the form being created in the code itself and not creating the objects through the UI and then addressing them in the code.
So, how do I either read text from or send text to (append) a Text Edit object that was not created in code but through the drag 'n' drop interface within QT Creator?
I tried the following but know that self. can't work, since the object was not created within the code itself. My added lines of interest are at the end of __init__
# This Python file uses the following encoding: utf-8 import os from pathlib import Path import sys from PySide2.QtWidgets import QApplication, QWidget from PySide2.QtCore import QFile from PySide2.QtUiTools import QUiLoader class Widget(QWidget): def __init__(self): super(Widget, self).__init__() self.load_ui() lfile = open("model.f90") for line in lfile: parts = line.split("=") self.dumpText.setText(parts) def load_ui(self): loader = QUiLoader() path = os.fspath(Path(__file__).resolve().parent / "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 = Widget() widget.show() sys.exit(app.exec_())
-
Hi,
Since you are using QUiLoader, you can then use the findChild method to retrieve the widgets created and then connect them using signals and slots.
-
Hi,
Since you are using QUiLoader, you can then use the findChild method to retrieve the widgets created and then connect them using signals and slots.
wrote on 24 May 2022, 18:45 last edited by@SGaist
May I ask for a little clarity on this?
FindChild method?
Connnect using signals and slots?Is there any documentation you might be able to point me to?
As stated, I have bought the latest ebook for QT6 and, obviously, have access to everything online; however, everything I have found deals only with coding directly and not with connecting the design interface to the code.
-
Just to be sure we understand each other correctly, what do you mean in practice by "connecting the design interface to the code." ?
-
Just to be sure we understand each other correctly, what do you mean in practice by "connecting the design interface to the code." ?
wrote on 24 May 2022, 19:32 last edited by@SGaist
As stated, I have designed my form within QT6 Creator, simply dragging and dropping in the input boxes, etc that I want to use.I then made changes to their properties, as required.
I then want to address these within the code but cannot seem to do so, since all the examples I have show the creating of these inputs via the code itself rather than as I have done it (and as QT6 Creator seems to want you to do).
Does that help?
-
Looks a bit like the Calculator Builder Example.
Note that the example has not yet been fully translated to Python.
-
@SGaist
As stated, I have designed my form within QT6 Creator, simply dragging and dropping in the input boxes, etc that I want to use.I then made changes to their properties, as required.
I then want to address these within the code but cannot seem to do so, since all the examples I have show the creating of these inputs via the code itself rather than as I have done it (and as QT6 Creator seems to want you to do).
Does that help?
wrote on 24 May 2022, 20:05 last edited by JonB@GaryN
Another example of this approach is shown in https://www.blog.pythonlibrary.org/2018/05/30/loading-ui-files-in-qt-for-python/.Are you aware that a quite different approach is to run
pyside2-uic
on the.ui
designer file to generate Python code which you include in your project? That defines a class and member variables for the Designer form and its widgets. https://stackoverflow.com/a/54847001/489865 discusses this way of working. Personally I prefer it. A bit more effort to set up, but much better experience from then onward.Have you read Using .ui files from Designer or QtCreator with QUiLoader and pyside2-uic? This shows the two approaches. I prefer Option A.
-
@GaryN
Another example of this approach is shown in https://www.blog.pythonlibrary.org/2018/05/30/loading-ui-files-in-qt-for-python/.Are you aware that a quite different approach is to run
pyside2-uic
on the.ui
designer file to generate Python code which you include in your project? That defines a class and member variables for the Designer form and its widgets. https://stackoverflow.com/a/54847001/489865 discusses this way of working. Personally I prefer it. A bit more effort to set up, but much better experience from then onward.Have you read Using .ui files from Designer or QtCreator with QUiLoader and pyside2-uic? This shows the two approaches. I prefer Option A.
wrote on 25 May 2022, 12:37 last edited by@JonB
Thank you.I tried the pyside2-uic approach, but this seemed to add complexity at this early stage.
However, it did (through your links) lead me to some interesting articles. One caused me to rewrite the code that Qt Creator created automatically. It appears that the default is now to create a Widget, referencing QWidget but that I can use QObject and, as stated by @SGaist, I can then use findChild.
I did this for my single QTextEdit item and the following code runs without error, but does not achieve what I am hoping for.
I would like the placeholderText that I have written into the ui interface (the text "test") to be replaced by the text "Successful Test"; as per the code.
# This Python file uses the following encoding: utf-8 import sys from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication, QTextEdit from PySide2.QtCore import QFile, QObject class Form(QObject): def __init__(self, ui_file, parent=None): super(Form, self).__init__(parent) ui_file = QFile(ui_file) ui_file.open(QFile.ReadOnly) loader = QUiLoader() self.window = loader.load(ui_file) self.dumpText = self.window.findChild(QTextEdit, 'dumpText') lfile = open("model.f90") for line in lfile: parts = line.split("=") self.dumpText.setText = "Successful Test" ui_file.close() self.window.show() if __name__ == "__main__": app = QApplication(sys.argv) form = Form('form.ui') sys.exit(app.exec_())
The interface I am working with (and the QTextEdit I am referencing) are shown here...
-
@JonB
Thank you.I tried the pyside2-uic approach, but this seemed to add complexity at this early stage.
However, it did (through your links) lead me to some interesting articles. One caused me to rewrite the code that Qt Creator created automatically. It appears that the default is now to create a Widget, referencing QWidget but that I can use QObject and, as stated by @SGaist, I can then use findChild.
I did this for my single QTextEdit item and the following code runs without error, but does not achieve what I am hoping for.
I would like the placeholderText that I have written into the ui interface (the text "test") to be replaced by the text "Successful Test"; as per the code.
# This Python file uses the following encoding: utf-8 import sys from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication, QTextEdit from PySide2.QtCore import QFile, QObject class Form(QObject): def __init__(self, ui_file, parent=None): super(Form, self).__init__(parent) ui_file = QFile(ui_file) ui_file.open(QFile.ReadOnly) loader = QUiLoader() self.window = loader.load(ui_file) self.dumpText = self.window.findChild(QTextEdit, 'dumpText') lfile = open("model.f90") for line in lfile: parts = line.split("=") self.dumpText.setText = "Successful Test" ui_file.close() self.window.show() if __name__ == "__main__": app = QApplication(sys.argv) form = Form('form.ui') sys.exit(app.exec_())
The interface I am working with (and the QTextEdit I am referencing) are shown here...
wrote on 25 May 2022, 18:31 last edited byIgnore me; got it sorted.
After recognising that I had the setText format incorrect, I have itall working.
Thank you!
1/9