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 access to ui members from the Designer in the QtCreator?
Forum Updated to NodeBB v4.3 + New Features

How to access to ui members from the Designer in the QtCreator?

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 797 Views 1 Watching
  • 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.
  • PowerNowP Offline
    PowerNowP Offline
    PowerNow
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      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
      1
      • PowerNowP Offline
        PowerNowP Offline
        PowerNow
        wrote on last edited by
        #3

        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().

        1. Generate a file
        $ pyuic5 -o main_window_ui.py ui/main_window.ui
        

        and then access the members via

        self.ui = Ui_MainWindow()
        
        1. 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?

        JonBJ 1 Reply Last reply
        0
        • PowerNowP PowerNow

          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().

          1. Generate a file
          $ pyuic5 -o main_window_ui.py ui/main_window.ui
          

          and then access the members via

          self.ui = Ui_MainWindow()
          
          1. 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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @PowerNow
          If you use the pyuic5 approach to "translate" the .ui file into a .py file --- which indeed I recommend --- you no longer use QUiLoader. They are an either-or approach. With pyuic5 you instead import 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.

          1 Reply Last reply
          0
          • PowerNowP Offline
            PowerNowP Offline
            PowerNow
            wrote on last edited by PowerNow
            #5

            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()".

            JonBJ 1 Reply Last reply
            0
            • PowerNowP PowerNow

              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()".

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @PowerNow
              If you wish to use QUiLoader() --- 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-description

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

              1 Reply Last reply
              1
              • PowerNowP Offline
                PowerNowP Offline
                PowerNow
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                1

                • Login

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