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

    jsulmJ 1 Reply Last reply
    0
    • C ClarkyBr

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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 last edited by ClarkyBr
        #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
        
        
        JonBJ jsulmJ 2 Replies Last reply
        0
        • C ClarkyBr

          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
          
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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

            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
            
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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 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

              • Login

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