Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to check QLabel exist or not in PyQT5



  • I am creating dynamically QLabel and QLineEdit, Before creating them i need to know if already label already exist or not. Please helpme, Thanks in Advance



  • @krsapril24
    You have to define what you mean by "if already label already exist or not.". Any label that is a child of such and such? A label with same text? A label where you previously used setObjectName() to something we can search for now?

    Or, maybe you mean: when you first create a widget you often save it in self (e.g. a QDialog), and you want to know if you have previously done so?

    If that's what you mean, you can do that by putting into your dialog's __init__():

    self.label = None
    self.lineedit = None
    

    and you can create first time only with:

    if not self.label:
        self.label = QLabel()
        self.lineedit = QLineEdit()
    

    Or what?



  • Hi created like this

    if(i==1):

                '''self.formLayout.removeWidget(self.childCmpName1LineEdit.hide())
                self.formLayout.removeWidget(self.childCmpName1)
                self.formLayout.removeWidget(self.parentCmpName2LineEdit)
                self.formLayout.removeWidget(self.parentCmpName2)
                self.formLayout.removeWidget(self.self.childCmpNameLineEdit)
                self.formLayout.removeWidget(self.self.childCmpName)
                self.formLayout.removeWidget(self.parentCmpName3LineEdit)
                self.formLayout.removeWidget(self.parentCmpName3)
                self.formLayout.removeWidget(self.branchNameLineEdit)
                self.formLayout.removeWidget(self.branchName)'''
    
                self.parentCmpName1 = QLabel(self.formLayoutWidget)
                self.parentCmpName1.setObjectName("parentCompanyName")
                self.parentCmpName1.setText("Parent Company Name* : ")
                self.formLayout.setWidget(13, QFormLayout.LabelRole, self.parentCmpName1)
                self.parentCmpName1LineEdit = QLineEdit(self.formLayoutWidget)
                self.formLayout.setWidget(13, QFormLayout.FieldRole, self.parentCmpName1LineEdit)
        elif(i==2):
            '''self.formLayout.removeWidget(self.parentCmpName1LineEdit)
            self.formLayout.removeWidget(self.parentCmpName1)'''
    
            if(self.parentCmpName1.hasSelectedText()):
                print("hi")
    
            '''if(self.parentCmpName1LineEdit):
                self.parentCmpName1LineEdit.hide()'''
    
            self.parentCmpName2 = QLabel(self.formLayoutWidget)
            self.parentCmpName2.setObjectName("parentCompanyName")
            self.parentCmpName2.setText("Parent Company Name* : ")
            self.formLayout.setWidget(13, QFormLayout.LabelRole, self.parentCmpName2)
            self.parentCmpName2LineEdit = QLineEdit(self.formLayoutWidget)
            self.formLayout.setWidget(13, QFormLayout.FieldRole, self.parentCmpName2LineEdit)
    
            self.childCmpName = QLabel(self.formLayoutWidget)
            self.childCmpName.setObjectName("childCompanyName")
            self.childCmpName.setText("Child Company Name* : ")
            self.formLayout.setWidget(14, QFormLayout.LabelRole, self.childCmpName)
            self.childCmpNameLineEdit = QLineEdit(self.formLayoutWidget)
            self.formLayout.setWidget(14, QFormLayout.FieldRole, self.childCmpNameLineEdit)
        elif(i==3):
            '''self.formLayout.removeWidget(self.parentCmpName1LineEdit)
            self.formLayout.removeWidget(self.parentCmpName1)
            self.formLayout.removeWidget(self.parentCmpName2LineEdit)
            self.formLayout.removeWidget(self.parentCmpName2)
            self.formLayout.removeWidget(self.self.childCmpNameLineEdit)
            self.formLayout.removeWidget(self.self.childCmpName)'''
    
            self.parentCmpName3 = QLabel(self.formLayoutWidget)
            self.parentCmpName3.setObjectName("parentCompanyName")
            self.parentCmpName3.setText("Parent Company Name* : ")
            self.formLayout.setWidget(13, QFormLayout.LabelRole, self.parentCmpName3)
            self.parentCmpName3LineEdit = QLineEdit(self.formLayoutWidget)
            self.formLayout.setWidget(13, QFormLayout.FieldRole, self.parentCmpName3LineEdit)
    
            self.childCmpName1 = QLabel(self.formLayoutWidget)
            self.childCmpName1.setObjectName("childCompanyName")
            self.childCmpName1.setText("Child Company Name* : ")
            self.formLayout.setWidget(14, QFormLayout.LabelRole, self.childCmpName1)
            self.childCmpName1LineEdit = QLineEdit(self.formLayoutWidget)
            self.formLayout.setWidget(14, QFormLayout.FieldRole, self.childCmpName1LineEdit)
    
            self.branchName = QLabel(self.formLayoutWidget)
            self.branchName.setObjectName("branchName")
            self.branchName.setText("Branch Name* : ")
            self.formLayout.setWidget(15, QFormLayout.LabelRole, self.branchName)
            self.branchNameLineEdit = QLineEdit(self.formLayoutWidget)
            self.formLayout.setWidget(15, QFormLayout.FieldRole, self.branchNameLineEdit)
    

    i want to remove repeated Qlabels and QLine edits on different combobox selection. Kindly help me.



  • @krsapril24
    Sorry, that's 100 lines of code. I don't which bit is wrong. You seem to be doing some removeWidget() and some creating of widgets, so what exactly is the problem?



  • see if user select parent company i need to display only compnay name and if user select child company then i need to display parent company and child company.

    so if user select only one at a time no problem it is working. if user select first child comapny then i need to hide parent company name if exist and then i need to display parent company and child company. in this situation i am facing problem. because parent company does not exist when select child company. so i need to hide if exist only. Please help me. iam struggling in this last 3 days.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Where do you get the information about these companies from ?


Log in to reply