PyQt5: How to retrieve a QLineEdit by its name ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: PyQt5: How to retrieve a QLineEdit by its name ? (/thread-30428.html) |
PyQt5: How to retrieve a QLineEdit by its name ? - arbiel - Oct-20-2020 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 RE: PyQt5: How to retrieve a QLineEdit by its name ? - deanhystad - Oct-20-2020 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. RE: PyQt5: How to retrieve a QLineEdit by its name ? - arbiel - Oct-21-2020 (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). 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 RE: PyQt5: How to retrieve a QLineEdit by its name ? - deanhystad - Oct-21-2020 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_() RE: PyQt5: How to retrieve a QLineEdit by its name ? - arbiel - Oct-21-2020 Thank you for the example which answers my query. Arbiel |