Pyqt5 - Inheriting information from one class to another. (Python)

Asked

Viewed 51 times

-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.

1 answer

0


If what you want is a secondary window that opens, takes a value and returns it to the main window, you can make your secondary window inherit from a QDialog. This class has a method called exec_ which makes this much easier - we just need to explain to the secondary window in which moment the dialogue is "accepted", linking for example the sign self.pushButton.clicked with the method self.accept, that we inherited from the QDialog.

Here’s an example that does what you want (I took the liberty of simplifying the code a little bit). Now every time the Tela1 is clicked, it creates the dialogue (Tela2) and expects it to be accepted (in this case, the button is clicked), and then deals with the value returned. I hope it is clear enough:

from sys import argv, exit
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QVBoxLayout, QWidget, QPushButton, QDialog


class Tela_1(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Tela 1 - TESTE")
        self.setGeometry(50, 50, 400, 400) 
        
        # CREATE LAYOUT
        mainLayout = QVBoxLayout()
        self.setLayout(mainLayout)
        
        # CREATE WIDGETS      
        self.texto_final = QLineEdit()
        self.botao_add = QPushButton("Add")

        # ADD WIDGETS TO LAYOUT
        mainLayout.addWidget(self.texto_final)
        mainLayout.addWidget(self.botao_add)      

        # ADD CONNECT WIDGETS
        self.botao_add.clicked.connect(self.abrir_janela2)
    
    def abrir_janela2(self):
        texto_retornado = Tela_2().exec_()
        if texto_retornado:
            self.texto_final.setText(texto_retornado)


class Tela_2(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Tela 2 - TESTE")
        self.setGeometry(50, 50, 200, 200)
        
        # CREATE LAYOUT
        mainLayout = QVBoxLayout()
        self.setLayout(mainLayout)

        # CREATE WIDGETS      
        self.label = QLabel("Digite algo e clique no botão")
        self.lineEdit = QLineEdit()
        self.pushButton = QPushButton("Adicionar dados")
        
        # ADD WIDGETS TO LAYOUT
        mainLayout.addWidget(self.label)
        mainLayout.addWidget(self.lineEdit)
        mainLayout.addWidget(self.pushButton)
        
        # CONNECT WIDGETS
        self.pushButton.clicked.connect(self.accept)
    
    def exec_(self):
        if super().exec_() == QDialog.Accepted:
            return self.lineEdit.text()
        

app = QApplication(argv)
w = Tela_1()
w.show()
exit(app.exec_())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.