Python Forum
[PyQt] How to clear multiple Qlineedit in a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to clear multiple Qlineedit in a loop
#7
Well you gave it a stab that I can respect ;) -- your method though was a bit overly complex and while it could be made to work there is a much simpler way of doing this. Because you have to define each of these objects via code then it is much more efficient to simply put them into the dictionary at the time of creation right after you create them as follows:

Note I removed all the extra inessential things that were not focused on this specific issue but you can easily add them back in if you want the full monty.

Oh and I also changed the name of your variable self.txtNames{} to something more appropriate to what it was and what it was holding self.dctTextHndl{} -- The dct part is short for Dictionary as opposed to a List or lst. TextHndl is short for Textbox Handle -- now it could have been abbreviated however you wanted but whatever you end up using you should do your best to stay consistent as this makes it easier to read your code a month or two down the road. Oh and keeping them short rather than verbose (dictionaryTextboxHandle) makes your code also easier to read as that stuff can get obscenely long.

from sys import exit   as sysExit

from PyQt5.QtCore    import QRegExp, Qt
from PyQt5.QtGui     import QRegExpValidator
# Widget Container Objects
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QDockWidget
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QGridLayout, QGroupBox, QLabel
# Widget Action Objects
from PyQt5.QtWidgets import QLineEdit, QPushButton, QAction, QStyleFactory

class CenterPanel(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)
        self.Parent = parent

        self.NumRegex = QRegExp("[0-9._]+")
        self.Validatr = QRegExpValidator(self.NumRegex)
        self.dctTextHndl = {}

        # -------
        self.lblName = QLabel()
        self.lblName.setText('Name')
 
        self.lblNamTyp = QLabel()
        self.lblNamTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.lblNamTyp.setText ('[-]')
 
        self.txtName = QLineEdit()
        self.dctTextHndl[0] = self.txtName
        # -------
        self.lblWeight = QLabel()
        self.lblWeight.setText ('Weight')
 
        self.lblWgtTyp = QLabel()
        self.lblWgtTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.lblWgtTyp.setText('[kg]')
 
        self.txtWeight = QLineEdit()
        self.txtWeight.setValidator(self.Validatr)
        self.dctTextHndl[1] = self.txtWeight
        # -------
        self.lblYear = QLabel()
        self.lblYear.setText('Year')
 
        self.lblYrTyp = QLabel()
        self.lblYrTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.lblYrTyp.setText ('[-]')
 
        self.txtYear = QLineEdit()
        self.txtYear.setInputMethodHints(Qt.ImhDigitsOnly)
        self.dctTextHndl[2] = self.txtYear
        # -------
        self.lblPlace = QLabel()
        self.lblPlace.setText('Place')
 
        self.lblPlcTyp = QLabel()
        self.lblPlcTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.lblPlcTyp.setText ('[-]')
 
        self.txtPlace = QLineEdit()
        self.txtPlace.setInputMethodHints(Qt.ImhDigitsOnly)
        self.dctTextHndl[3] = self.txtPlace
        # -------
        grdTest = QGridLayout()
        grdTest.addWidget(self.lblName, 0, 0)
        grdTest.addWidget(QLabel('     '), 0, 1)
        grdTest.addWidget(self.lblNamTyp, 0, 2)
        grdTest.addWidget(self.txtName, 0, 3)
 
        grdTest.addWidget(self.lblWeight, 1, 0)
        grdTest.addWidget(QLabel('     '), 1, 1)
        grdTest.addWidget(self.lblWgtTyp, 1, 2)
        grdTest.addWidget(self.txtWeight, 1, 3)
 
        grdTest.addWidget(self.lblYear, 2, 0)
        grdTest.addWidget(QLabel('     '), 2, 1)
        grdTest.addWidget(self.lblYrTyp, 2, 2)
        grdTest.addWidget(self.txtYear, 2, 3)
 
        grdTest.addWidget(self.lblPlace, 3, 0)
        grdTest.addWidget(QLabel('     '), 3, 1)
        grdTest.addWidget(self.lblPlcTyp, 3, 2)
        grdTest.addWidget(self.txtPlace, 3, 3)
        # -------
        gbxTest = QGroupBox()
        gbxTest.setTitle('Test')
        gbxTest.setLayout(grdTest)
        # -------
        self.lblHidden = QLabel('')
        self.HideSet = False
 
        self.btnAddNew = QPushButton()
        self.btnAddNew.setText('Add New')
        self.btnAddNew.clicked.connect(self.ClearValues)
 
        hbxAddNew = QHBoxLayout()
        hbxAddNew.addWidget(self.lblHidden)
        hbxAddNew.addStretch(2)
        hbxAddNew.addWidget(self.btnAddNew)
        # -------
        vbxAll = QVBoxLayout()
        vbxAll.addWidget(gbxTest)
        vbxAll.addLayout(hbxAddNew)
        # -------
        gbxDetails = QGroupBox()
        gbxDetails.setTitle('Details')
        gbxDetails.setLayout(vbxAll)
        # -------
        hbxFinal = QHBoxLayout()
        hbxFinal.addWidget(gbxDetails)
        # -------
        self.setLayout(hbxFinal)
 
    def ClearValues(self):
        for idx in range(0, 4):
            self.dctTextHndl[idx].clear()
            
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
     
        self.setWindowTitle('Main')
 
        WinLeft = 150; WinTop = 150; WinWidth = 340; WinHight = 220
        self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
 
        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)
        self.setStyle(QStyleFactory.create('Cleanlooks'))
  
if __name__ == "__main__":
    MainThred = QApplication([])
 
    MainGui = MainWindow()
    MainGui.show()
 
    sysExit(MainThred.exec_())
So as you can see each time a Text Box object is created its handle is put into the object dictionary using a numeric key (any key will work but the numeric one is clearest and easiest when iterating through the dictionary later on) but this is not always the case -- just works best for this one. Still you could have done that function as follows -- which might have been better as its more generic and more pythonic:

    def ClearValues(self):
        for Key in self.dctTextHndl.keys():
            self.dctTextHndl[Key].clear()
Then in the ClearValues function we iterate through the dictionary using the Idx key (or just the Key in the 2nd one) which gives us the object handle and once we have that handle to the Text object we call that objects .clear() method.
Reply


Messages In This Thread
RE: How to clear multiple Qlineedit in a loop - by Denni - Aug-15-2019, 02:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,834 Nov-04-2022, 09:04 PM
Last Post: malonn
  How to accept only float number in QLineEdit input ism 5 28,482 Jul-06-2021, 05:23 PM
Last Post: deanhystad
  Simple printing the text for a QLineEdit thewolf 16 7,891 Mar-06-2021, 11:37 PM
Last Post: deanhystad
  PyQt5: How to retrieve a QLineEdit by its name ? arbiel 4 7,924 Oct-21-2020, 02:35 PM
Last Post: arbiel
  Two QlineEdit box, which interrelated GMCobraz 1 2,431 Aug-14-2020, 07:15 PM
Last Post: deanhystad
  [PyQt] Dynamically add and remove QLineEdit's GMCobraz 3 7,178 Jun-23-2020, 07:01 PM
Last Post: Yoriz
  prompt for input in qlineedit GMCobraz 3 3,233 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,326 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 6,722 Apr-03-2020, 12:15 AM
Last Post: JayCee
  How to validate multiple QLineEdit widgets without addressing them separately? mart79 3 4,272 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