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. Dynamic adding of textbox in pyqt5
Forum Update on Monday, May 27th 2025

Dynamic adding of textbox in pyqt5

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 4 Posters 4.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.
  • E Offline
    E Offline
    elias_hh
    wrote on 10 Jun 2020, 08:41 last edited by
    #1

    hi, guys,

    i have a question about a gui project. If the user selects one or more images on the gui, the names of the images should be displayed in text boxes. Currently i have implemented 3 textboxes statically. Therefore only the names of 3 images are displayed. Is there a way to make this more dynamic ? For example, if the user selects more than 3, the gui will be extended by two more textboxes ?

    I build the gui with qt designer. In the image, you can see the prototype guiUnbenannt.PNG . And this is the code:

        def __init__(self):
            super(Fenster, self).__init__()
            loadUi(r'C:\Users\NAME\Desktop\xyz\Code_Fr_Forum\test_.ui', self)
    
            self.pushButton.clicked.connect(self.load_img)
    
        def load_img(self):
            filename, _ = QFileDialog.getOpenFileNames(self, 'Select Multi File', 'default', 'All Files (*)')
            count = len(filename)
            #Myidea: Depend on the number of filenames, add more than 3 lineEdit on 
            #the GUI (dynamic). 
            #![alt text](![image url](![image url](image url)))Now its very static
            for index, name in enumerate(filename, start=1):
                # extract file name
                basename = os.path.basename(name)
                filename_list.append(basename)
    
                if index == 1: 
                    self.lineEdit.setText(basename)
    
                elif index == 2               
                    self.lineEdit_1.setText(basename)
    
                elif index == 3 and idx != -1:
                    self.lineEdit_2.setText(basename)
                
               #elif index > 3:
                    #add self.lineEdit_3 under self.lineEdit_2
                         self.lineEdit_3.setText(basename)
    
    
    ######MAIN####################
    app = QApplication(sys.argv)
    w = Fenster()
    w.show()
    sys.exit(app.exec_())```
    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 Jun 2020, 08:47 last edited by
      #2

      @elias_hh said in Dynamic adding of textbox in pyqt5:

      Is there a way to make this more dynamic ?

      Of course. You can add a list member variable where you store your line edits.

      for index, name in enumerate(filename, start=1):
          # extract file name
          basename = os.path.basename(name)
          filename_list.append(basename)
          self.lineEdits += QLineEdit(self) # Make sure to set correct parent
          # Use the line edit you just added
      

      Use a layout to organise all this widgets.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply 15 Jun 2020, 08:48
      2
      • E Offline
        E Offline
        elias_hh
        wrote on 15 Jun 2020, 08:33 last edited by
        #3

        @Denni-0 Hey, I thank you for the very detailed and helpful MUC. My later GUI looks much more complex than in the photo, which I showed as an example. Therefore I create the GUI with QT Designer (static). Is there still the possibility to change the GUI I created with QT Designer dynamically and add some textboxes to my main program ?

        With your MUC it is possible, but is it also possible to do this with a GUI that was created with Designer ?

        J 1 Reply Last reply 15 Jun 2020, 08:49
        0
        • J jsulm
          10 Jun 2020, 08:47

          @elias_hh said in Dynamic adding of textbox in pyqt5:

          Is there a way to make this more dynamic ?

          Of course. You can add a list member variable where you store your line edits.

          for index, name in enumerate(filename, start=1):
              # extract file name
              basename = os.path.basename(name)
              filename_list.append(basename)
              self.lineEdits += QLineEdit(self) # Make sure to set correct parent
              # Use the line edit you just added
          

          Use a layout to organise all this widgets.

          E Offline
          E Offline
          elias_hh
          wrote on 15 Jun 2020, 08:48 last edited by
          #4

          @jsulm thank u very much. Would this also work if my GUI was created with QT Designer ? So I can overwrite the GUI with self.lineEdits += QLineEdit(self) afterwards with several textbox's ?

          J 1 Reply Last reply 15 Jun 2020, 10:30
          0
          • E elias_hh
            15 Jun 2020, 08:33

            @Denni-0 Hey, I thank you for the very detailed and helpful MUC. My later GUI looks much more complex than in the photo, which I showed as an example. Therefore I create the GUI with QT Designer (static). Is there still the possibility to change the GUI I created with QT Designer dynamically and add some textboxes to my main program ?

            With your MUC it is possible, but is it also possible to do this with a GUI that was created with Designer ?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 15 Jun 2020, 08:49 last edited by
            #5

            @elias_hh I think if you want to modify your UI at runtime you will need to change your approach: do not use loadUi but instead use pyuic to generate Python code from ui file (which is XML).
            Take a look at https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • E elias_hh
              15 Jun 2020, 08:48

              @jsulm thank u very much. Would this also work if my GUI was created with QT Designer ? So I can overwrite the GUI with self.lineEdits += QLineEdit(self) afterwards with several textbox's ?

              J Offline
              J Offline
              JonB
              wrote on 15 Jun 2020, 10:30 last edited by
              #6

              @elias_hh
              I would recommend doing as @jsulm says. It is then easy to mix design-time & run-time widgets as desired, if that's what you want to do.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                elias_hh
                wrote on 15 Jun 2020, 11:22 last edited by
                #7

                Hey, guys,

                Thanks for your help. I have decided to completely rethink my approach and work without Qt Designer. I will use the code from @Denni-0 as a base because it is almost exactly the way I want it to be. My gui should look like the following picture (designed with qt designer): Unbenannt.PNG

                My idea is that the program filters the required parameters from the names of the images. I know how to do that and it works. But as you can see in the picture I need two more QLineEdits(). One for Heights and one for Width. These act as security to see if the images are named correctly. If the user has named the images according to the correct nomenclature, the parameters appear there. If the image names were named incorrectly, a question mark appears there.

                But my main problem now is, I don't know how to position these textboxes next to the image name. I can only do it below. I tried with xyz.move(x,y) but the textboxes are still below. How can i solve this ?

                J 1 Reply Last reply 15 Jun 2020, 11:41
                0
                • E elias_hh
                  15 Jun 2020, 11:22

                  Hey, guys,

                  Thanks for your help. I have decided to completely rethink my approach and work without Qt Designer. I will use the code from @Denni-0 as a base because it is almost exactly the way I want it to be. My gui should look like the following picture (designed with qt designer): Unbenannt.PNG

                  My idea is that the program filters the required parameters from the names of the images. I know how to do that and it works. But as you can see in the picture I need two more QLineEdits(). One for Heights and one for Width. These act as security to see if the images are named correctly. If the user has named the images according to the correct nomenclature, the parameters appear there. If the image names were named incorrectly, a question mark appears there.

                  But my main problem now is, I don't know how to position these textboxes next to the image name. I can only do it below. I tried with xyz.move(x,y) but the textboxes are still below. How can i solve this ?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 15 Jun 2020, 11:41 last edited by
                  #8

                  @elias_hh said in Dynamic adding of textbox in pyqt5:

                  how to position these textboxes next to the image name

                  You need to put them into the correct layout, don't use move().
                  Please show your code and where you want to add text boxes.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  E 1 Reply Last reply 15 Jun 2020, 13:06
                  0
                  • J jsulm
                    15 Jun 2020, 11:41

                    @elias_hh said in Dynamic adding of textbox in pyqt5:

                    how to position these textboxes next to the image name

                    You need to put them into the correct layout, don't use move().
                    Please show your code and where you want to add text boxes.

                    E Offline
                    E Offline
                    elias_hh
                    wrote on 15 Jun 2020, 13:06 last edited by
                    #9

                    @jsulm i threw out my code and want to use @Denni-0 . The only difference is that I want to have two more textboxes next to the textbox with the name (see figure).

                    1 Reply Last reply
                    0

                    1/9

                    10 Jun 2020, 08:41

                    • 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