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 can I add other widgets by pressing a button on PyQt5?
QtWS25 Last Chance

How can I add other widgets by pressing a button on PyQt5?

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 440 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.
  • C Offline
    C Offline
    ClarkyBr
    wrote on 9 Jan 2025, 16:50 last edited by
    #1

    Basically I'm trying to make the "Add New Row" to create another row exactly like the others above and with all those widgets (timeEdit, comboBox, doubleSpinBox). The user will add as many as they want based on their necessities.

    Where do I even begin to code the button's method and how can I make use of the newly generated objects' data?

    Screenshot from 2025-01-09 13-27-39.png

    I searched online but couldn't find much, and I don't know if this is possible on PyQt 5.

    J 1 Reply Last reply 9 Jan 2025, 16:59
    0
    • C ClarkyBr
      9 Jan 2025, 16:50

      Basically I'm trying to make the "Add New Row" to create another row exactly like the others above and with all those widgets (timeEdit, comboBox, doubleSpinBox). The user will add as many as they want based on their necessities.

      Where do I even begin to code the button's method and how can I make use of the newly generated objects' data?

      Screenshot from 2025-01-09 13-27-39.png

      I searched online but couldn't find much, and I don't know if this is possible on PyQt 5.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 9 Jan 2025, 16:59 last edited by
      #2

      @ClarkyBr said in How can I add other widgets by pressing a button on PyQt5?:

      Where do I even begin to code the button's method

      Do you already know how signals/slots work?
      Connect a slot to buttons clicked() signal. In that slot create the widgets you need and add them to your layout.

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

      1 Reply Last reply
      2
      • C Offline
        C Offline
        ClarkyBr
        wrote on 9 Jan 2025, 18:38 last edited by ClarkyBr 1 Sept 2025, 18:39
        #3

        Yes, I know, I did it like this:

        class Ui_MainWindow(object):
            def setupUi(self, MainWindow):
                # ...
                self.addRowButton.clicked.connect(self.clicked)
                # ...
        
            def clicked(self):
               # This is the part where I'm struggling
        
        
        J J 2 Replies Last reply 9 Jan 2025, 19:19
        0
        • C ClarkyBr
          9 Jan 2025, 18:38

          Yes, I know, I did it like this:

          class Ui_MainWindow(object):
              def setupUi(self, MainWindow):
                  # ...
                  self.addRowButton.clicked.connect(self.clicked)
                  # ...
          
              def clicked(self):
                 # This is the part where I'm struggling
          
          
          J Offline
          J Offline
          JonB
          wrote on 9 Jan 2025, 19:19 last edited by
          #4

          @ClarkyBr
          In the slot you would append a new row to whatever layout type you are using and create new instances of widgets to put there. You will need to understand how to code to create widgets and add them to layouts.

          As a separate point you probably ought to do this using a QTreeWidget or QTreeView, which have columns and allow for row insertion.

          1 Reply Last reply
          1
          • C ClarkyBr
            9 Jan 2025, 18:38

            Yes, I know, I did it like this:

            class Ui_MainWindow(object):
                def setupUi(self, MainWindow):
                    # ...
                    self.addRowButton.clicked.connect(self.clicked)
                    # ...
            
                def clicked(self):
                   # This is the part where I'm struggling
            
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 10 Jan 2025, 06:03 last edited by
            #5

            @ClarkyBr said in How can I add other widgets by pressing a button on PyQt5?:

            This is the part where I'm struggling

            What exactly is the Problem?
            You create widgets like any other objects in Python and then add them to the layout.
            Take a look at the examples: https://github.com/pyqt/examples?tab=readme-ov-file

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ClarkyBr
              wrote on 12 Jan 2025, 16:18 last edited by
              #6

              Thank you for all the help guys! I just needed to search about "Adding new widgets dynamically", but I didn't know it was called like this. This is the final result:

              def addRow(self, r):
                 
                     self.newLayoutRow = QtWidgets.QHBoxLayout()
                 
                     self.newLayoutRow.setObjectName("newLayoutRow_" + str(r))
                 
                     self.newTimeEdit = QtWidgets.QTimeEdit(self.sorting0)
                     self.newTimeEdit.setObjectName("newTimeEdit_" + str(r))
                     self.mainGrid.addWidget(self.newTimeEdit, r, 0, 1, 1)
                 
                     self.QComboBox1 = QtWidgets.QComboBox(self.sorting0)
                     self.QComboBox1.setObjectName("QComboBox1_" + str(r))
                     self.newLayoutRow.addWidget(self.QComboBox1)
                 
                     self.QComboBox2 = QtWidgets.QComboBox(self.sorting0)
                     self.QComboBox2.setObjectName("QComboBox2_" + str(r))
                     self.newLayoutRow.addWidget(self.QComboBox2)
                 
                     self.QSpinBox = QtWidgets.QSpinBox(self.sorting0)
                     self.QSpinBox.setObjectName("QSpinBox_" + str(r))
                     self.newLayoutRow.addWidget(self.QSpinBox)
                 
                     self.mainGrid.addLayout(self.newLayoutRow, r, 1, 1, 1)
              

              Now just need to add some for loops and it's good to go

              1 Reply Last reply
              0

              5/6

              10 Jan 2025, 06:03

              • Login

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