Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to connect UI to code
Forum Update on Monday, May 27th 2025

How to connect UI to code

Scheduled Pinned Locked Moved Solved Qt for Python
9 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GaryN
    wrote on 23 May 2022, 19:45 last edited by
    #1

    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_())
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 May 2022, 19:57 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply 24 May 2022, 18:45
      2
      • S SGaist
        23 May 2022, 19:57

        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.

        G Offline
        G Offline
        GaryN
        wrote on 24 May 2022, 18:45 last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 May 2022, 18:52 last edited by
          #4

          Just to be sure we understand each other correctly, what do you mean in practice by "connecting the design interface to the code." ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply 24 May 2022, 19:32
          0
          • S SGaist
            24 May 2022, 18:52

            Just to be sure we understand each other correctly, what do you mean in practice by "connecting the design interface to the code." ?

            G Offline
            G Offline
            GaryN
            wrote on 24 May 2022, 19:32 last edited by
            #5

            @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?

            J 1 Reply Last reply 24 May 2022, 20:05
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 24 May 2022, 19:48 last edited by
              #6

              Looks a bit like the Calculator Builder Example.

              Note that the example has not yet been fully translated to Python.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • G GaryN
                24 May 2022, 19:32

                @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?

                J Offline
                J Offline
                JonB
                wrote on 24 May 2022, 20:05 last edited by JonB
                #7

                @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.

                G 1 Reply Last reply 25 May 2022, 12:37
                1
                • J JonB
                  24 May 2022, 20:05

                  @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.

                  G Offline
                  G Offline
                  GaryN
                  wrote on 25 May 2022, 12:37 last edited by
                  #8

                  @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...

                  alt text

                  G 1 Reply Last reply 25 May 2022, 18:31
                  0
                  • G GaryN
                    25 May 2022, 12:37

                    @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...

                    alt text

                    G Offline
                    G Offline
                    GaryN
                    wrote on 25 May 2022, 18:31 last edited by
                    #9

                    Ignore me; got it sorted.

                    After recognising that I had the setText format incorrect, I have itall working.

                    Thank you!

                    1 Reply Last reply
                    0

                    1/9

                    23 May 2022, 19:45

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved