Python Forum
How to validate multiple QLineEdit widgets without addressing them separately?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to validate multiple QLineEdit widgets without addressing them separately?
#1
All,

At the moment I have about 30 QLineEdit widgets on my GUI. The snippet only shows 3. Do I need to address them all individually or is there something which is more efficient?

        
from PyQt5 import QtCore, QtGui, QtWidgets
from propertyDialog5 import Ui_Dialog
from PyQt5.QtWidgets import  QApplication, QTreeWidget, QTreeWidgetItem,  QComboBox
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
import numpy as np


[python]class mainProgram(QtWidgets.QMainWindow, Ui_Dialog):
    def __init__(self, parent=None):
        self.cntPipe = 0
        self.pipeproperty = {}
        self.joint_weight = 0
        self.weight_pipe = 0
        self.weight_liner = 0
        self.weight_coating = 0
        self.pipe = 0
        self.specific_gravity = 9.81
        self.total_coating_weight = 0
        self.total_liner_weight = 0
        self.txtAbsorptionType.addItems(['Mass','Volume'])
        self.txtAbsorptionType.setCurrentIndex(0)
        self.AbsorptionType = 'Mass'
        
        super(mainProgram, self).__init__(parent)
        self.setupUi(self)


        regex = QRegExp ("[0-9._]+")
        validator = QRegExpValidator(regex)
        self.txtPipeOuterDiameter.setValidator(validator)
        self.txtPipeInnerDiameter.setValidator(validator)
        self.txtPipeDensity.setValidator(validator)
        

if __name__ == "__main__":

    import sys
    app = QtWidgets.QApplication(sys.argv)
    nextGui = mainProgram()
    nextGui.show()
    sys.exit(app.exec_())
Reply
#2
Use a dictionary to store a handle to the QLineEdit and then reference them via the Dictionary. Further if you make the QLineEdit a class you can handle it more efficiently within code -- assuming you are doing anything with those QLineEdits beyond the basics.
Reply
#3
(Aug-07-2019, 12:43 PM)Denni Wrote: Use a dictionary to store a handle to the QLineEdit and then reference them via the Dictionary. Further if you make the QLineEdit a class you can handle it more efficiently within code -- assuming you are doing anything with those QLineEdits beyond the basics.

Thanks for the advice. I am still learning to master Python. Could you steer me in the right direction with a small example so I can alter/use this on my code?
Reply
#4
Do you know how to use a Dictionary? If yes then its just - a matter of doing something like -- Dict[key] = self.LineEdit
Reply


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