-2
I created two windows in Pyqt5, where the first window needs to inherit information from the second, in this example I created:
- Tela_1: containing a "Qlineedit" for typing and a "Qpushbutton" to open the other window.
- Tela_2: also contains a "Qlineedit" for typing and a "Qpushbutton" to take the typed text information to window 1
In the example below the class Tela_1 has a function with the name "complete_fields" that will receive information about what was typed in "Qlineedit" of Tela_2 and complete this information on "Qlineedit" of Tela_1
from sys import argv, exit
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QVBoxLayout, QWidget, QPushButton
class Tela_1(QWidget):
def __init__(self):
super(Tela_1, self).__init__()
self.setWindowTitle("Tela 1 - TESTE")
self.setGeometry(50, 50, 400, 400)
self.janela2 = Tela_2()
# ADD WIDGETS
self.texto_final = QLineEdit()
self.botao_add = QPushButton()
self.botao_add.setText("Add")
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.texto_final)
mainLayout.addWidget(self.botao_add)
self.setLayout(mainLayout)
self.botao_add.clicked.connect(self.abrir_janela2)
def abrir_janela2(self):
self.janela2.show()
def completa_campo(self, texto):
print("O valor digitado foi: ", texto)
self.texto_final.setText(texto)
class Tela_2(QWidget):
def __init__(self):
super(Tela_2, self).__init__()
self.setWindowTitle("Tela 2 - TESTE")
self.setGeometry(50, 50, 200, 200)
# ADD WIDGETS
self.label = QLabel()
self.label.setText("Digite algo e clique no botão")
self.lineEdit = QLineEdit()
self.pushButton = QPushButton()
self.pushButton.setText("Adicionar dados")
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.label)
mainLayout.addWidget(self.lineEdit)
mainLayout.addWidget(self.pushButton)
self.setLayout(mainLayout)
self.pushButton.clicked.connect(self.envia_dados)
def envia_dados(self):
self.janela_1 = Tela_1()
dados = self.lineEdit.text()
if dados != "":
self.janela_1.completa_campo(texto=dados)
self.hide()
app = QApplication(argv)
w = Tela_1()
w.show()
exit(app.exec_())
In the code above is not bringing this information, he can even print on the screen what was typed, but does not complete the "Qlineedit" when using. setText().
How do I inherit information from a "Qlineedit" from another window without starting current window again?
Hi Rafael, just an addendum: the most accurate term would be "transporting information between Widgets". When we talk about "inheriting" within programming, we soon think of derived classes inheriting their methods and attributes from their respective basic classes, which is not what you want here.
– jfaccioni