Python Forum
[PyQt] Dynamically add and remove QLineEdit's
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Dynamically add and remove QLineEdit's
#1
Dear all,

Anyone got idea how to add complete new blank row as previous row?
Without do it manually or add in GUI first.

For example like fill in information for a family member.
We not sure the exact number of members.

So we can add or remove additional row in the program, by clicking add button or remove button.

Please give me some hints.
I am using PySide2 as tool to create the GUI.

Thanks
Reply
#2
Please share your code, It really is required in order to provide a good answer.
Reply
#3
Dear all,

This is one example that I copy from a website.
I wish to duplicate the row without duplicating like name1, name 2 in the code and show in GUI.

I want initial with one row. But with add or remove button, I can simply duplicate or remove the row.

May I get some hints from you guys.

Thanks.


import sys
from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PySide2.QtWidgets import QPushButton
from PySide2.QtCore import QSize


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(320, 140))
        self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")

        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Name:')
        self.line = QLineEdit(self)

        self.line.move(80, 20)
        self.line.resize(200, 32)
        self.nameLabel.move(20, 20)

        pybutton = QPushButton('OK', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(200,32)
        pybutton.move(80, 60)

    def clickMethod(self):
        print('Your name: ' + self.line.text())

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())
Reply
#4
I have had a go but keep in mind that I don't use pyside/pyqt so its just thrown together from looking at random tutorials.
The frame does not resize based on the widgets, I didn't figure that out yet.
Note: I had PyQt5 installed with anaconda so I just changed all the imports, hopefully, it works in PySide2 too.

import sys
from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit, QFormLayout
from PySide2.QtWidgets import QPushButton, QVBoxLayout, QHBoxLayout
from PySide2.QtCore import QSize


class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.setMinimumSize(QSize(320, 220))
        self.setWindowTitle(
            "PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")
        self.controls = []
        button1 = QPushButton('add field', self)
        button1.clicked.connect(self.add_field)
        button2 = QPushButton('remove field', self)
        button2.clicked.connect(self.remove_field)
        button3 = QPushButton('OK', self)
        button3.clicked.connect(self.clickMethod)

        vbox = QVBoxLayout()
        formlayout = QFormLayout()
        vbox.addLayout(formlayout)
        hbox = QHBoxLayout()
        hbox.addWidget(button1)
        hbox.addWidget(button2)
        vbox.addLayout(hbox)
        vbox.addWidget(button3)
        self.setLayout(vbox)
        self.formlayout = formlayout
        self.add_field()

    def add_field(self):
        edit = QLineEdit(self)
        self.controls.append(edit)
        self.formlayout.addRow('Name', edit)

    def remove_field(self):
        if len(self.controls) > 1:
            edit = self.controls.pop()
            self.formlayout.removeRow(edit)

    def clickMethod(self):
        names = ', '.join(edit.text() for edit in self.controls)
        print(f'Names: {names}')


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,738 Nov-04-2022, 09:04 PM
Last Post: malonn
  How to accept only float number in QLineEdit input ism 5 28,127 Jul-06-2021, 05:23 PM
Last Post: deanhystad
  Simple printing the text for a QLineEdit thewolf 16 7,659 Mar-06-2021, 11:37 PM
Last Post: deanhystad
  PyQt5: How to retrieve a QLineEdit by its name ? arbiel 4 7,752 Oct-21-2020, 02:35 PM
Last Post: arbiel
  Two QlineEdit box, which interrelated GMCobraz 1 2,372 Aug-14-2020, 07:15 PM
Last Post: deanhystad
  prompt for input in qlineedit GMCobraz 3 3,160 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,254 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 6,596 Apr-03-2020, 12:15 AM
Last Post: JayCee
  [PyQt] How to clear multiple Qlineedit in a loop mart79 6 7,642 Aug-15-2019, 02:37 PM
Last Post: Denni
  How to validate multiple QLineEdit widgets without addressing them separately? mart79 3 4,186 Aug-08-2019, 12:50 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020