Python Forum
PyQt5: How to retrieve a QLineEdit by its name ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5: How to retrieve a QLineEdit by its name ?
#1
Hi

I want to update a html file containing element such as :

<auteur id="Alc" auteur="Alcée" origine="Mytilène" genre="poète lyrique" title="vers 610 avant J.-C."><a href="Alcée_de_Mytilène"></a></auteur>

I'm writing a code to create within a loop several QLabel and QLineEdit whithout naming them. Here is the code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QMetaObject, pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel,  QLineEdit,  QPushButton, QApplication
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QMessageBox
from bs4 import BeautifulSoup as btfs

class Auteur(QMainWindow):
	def __init__(auto):
		super(Auteur,auto).__init__()
		print("Auteur.__init__",type(auto))
	def init(auto):
		print("Auteur.init",type(auto))
		auto.setGeometry(250,200,800,300)
		auto.setWindowTitle('Auteur')
		auto.Fenster=QWidget()
		auto.setCentralWidget(auto.Fenster)
		zs=QHBoxLayout()
		libellés=QVBoxLayout()
		ligneXml=QVBoxLayout()
		zs.addLayout(libellés)
		zs.addLayout(ligneXml)
		l=QLabel('Identifiant')
		libellés.addWidget(l)
		auto.id=QLineEdit()
# creating the html arguments of <auteur> to be updated 
		for tpl in [('Nom','auteur'), ('Origine', 'origine'), ('Période', 'title'), ('Genres littéraires', 'genre'), ('Œuvres', 'œuvres'),('wikipedia', 'href') ]:
			l=QLabel(tpl[0])
			libellés.addWidget(l)
			argXml=QLineEdit()
			argXml.setObjectName(tpl[1])
			ligneXml.addWidget(argXml)
		zc=QHBoxLayout()
		zc.addStretch()
		for tpl in [('Quitter','quit', True), ('Wikipédia', 'wiki', False), ('Mémoriser', 'memo', False), ('Abandonner', 'abandon', False), ('Réinitialiser', 'reinit', False)]:
			btn=QPushButton(tpl[0], auto.Fenster)
			btn.setObjectName(tpl[1])
			btn.setEnabled(tpl[2])
			zc.addWidget(btn)
			zc.addStretch()
		vbl=QVBoxLayout(auto.Fenster)
		vbl.addLayout(zs)
		vbl.addStretch()
		vbl.addLayout(zc)
		QMetaObject.connectSlotsByName(auto)
	@pyqtSlot()
	def on_quit_clicked(auto):
		print("on_quitter_clicked",type(auto))
		auto.close()
	@pyqtSlot()
	def on_id_editingFinished(auto):
		try:
#			recherche de l'auteur dans le fichier html et affichage des informations
			auto.état='initial'
#			print(lauteur)
		except:
			auto.état='nouveau'
#			réinitialisation des rubriques de l'auteur
			pass
			return 0
	@pyqtSlot()
	def on_auteur_editingFinished(auto):
		print ('on_auteur_editingFinished')
	@pyqtSlot()
	def on_origine_editingFinished(auto):
		print ('on_origine_editingFinished')
def main(args):
	app = QtWidgets.QApplication(args)
	auteur=Auteur()
	auteur.init()
	auteur.show()
	app.exec()
	return 0

if __name__ == '__main__':
	import sys
	sys.exit(main(sys.argv))
	
How is it possible to retrieve each QEditLine by its name, to match it with the html argument with the same name ?

Arbiel
Reply
#2
Use objectName() to get the identifier name. To find a widget if you have the identifier name use findChild(object_type, id_name).

I don't know why you don't just assign a name. That way you know what they are called.
Reply
#3
(Oct-20-2020, 09:55 PM)deanhystad Wrote: Use objectName() to get the identifier name. To find a widget if you have the identifier name use findChild(object_type, id_name).

I don't know why you don't just assign a name. That way you know what they are called.

Thank you for your answer.

I knew about findChild, but I have not been able to use it properly. What value should I use for "object_type" ? QLineEdit apparently does not work.

I do not assign them a name because I create them in a loop, and I don't how to do this in such a context.

Arbiel
Reply
#4
from PySide2 import QtWidgets

class MyWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self.content = QtWidgets.QGridLayout(self)

        self.editors = []
        for i in range(4):
            editor = QtWidgets.QLineEdit(self)
            editor.setObjectName(f'lineEdit{i}')
            self.content.addWidget(editor, i, 0)
            self.editors.append(editor)
        self.editors[0].setText('lineEdit2')
            
        self.button = QtWidgets.QPushButton(self)
        self.button.setText('Say Hello')
        self.button.clicked.connect(self.find_child)
        self.content.addWidget(self.button, 4, 0)

    def find_child(self):
        objname = self.editors[0].text()
        child = self.findChild(QtWidgets.QLineEdit, objname)
        child.setText('Hello')

app = QtWidgets.QApplication()
w = MyWindow()
w.show()
app.exec_()
Reply
#5
Thank you for the example which answers my query.

Arbiel
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,730 Nov-04-2022, 09:04 PM
Last Post: malonn
  How to accept only float number in QLineEdit input ism 5 28,114 Jul-06-2021, 05:23 PM
Last Post: deanhystad
  Simple printing the text for a QLineEdit thewolf 16 7,654 Mar-06-2021, 11:37 PM
Last Post: deanhystad
  Two QlineEdit box, which interrelated GMCobraz 1 2,369 Aug-14-2020, 07:15 PM
Last Post: deanhystad
  [PyQt] Dynamically add and remove QLineEdit's GMCobraz 3 7,086 Jun-23-2020, 07:01 PM
Last Post: Yoriz
  prompt for input in qlineedit GMCobraz 3 3,156 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,250 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 6,595 Apr-03-2020, 12:15 AM
Last Post: JayCee
  [PyQt] How to clear multiple Qlineedit in a loop mart79 6 7,636 Aug-15-2019, 02:37 PM
Last Post: Denni
  How to validate multiple QLineEdit widgets without addressing them separately? mart79 3 4,182 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