python-3.xclasspyqtqlineedit

Updating/clearing the PyQt form using confirmation window


I would like that by typing anything in LineEdit in the first window and by pressing 'Confirm clearing' button in the second window, the first window would be updated so that the LineEdit field is emptied and focused.

import sys

from PyQt6.QtWidgets import (
    QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout
)


class Vw(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(1000, 400)
        self.setContentsMargins(30, 30, 30, 30)

        self.grid = QGridLayout(self)
        self.setLayout(self.grid)

        self.t1 = QLineEdit(self)
        self.grid.addWidget(self.t1, 0, 1)

        self.bOk = QPushButton("Clear the line edit", self)
        self.bOk.setFixedSize(120, 30)
        self.grid.addWidget(self.bOk, 5, 0)
        self.bOk.clicked.connect(self.implementing)
        self.bOk.setAutoDefault(True)

    def implementing(self):
        # to be added a code
        done()

    def clearing(self):
        self.t1.clear()
        self.t1.setFocus()


def done():
    Vw.wda = WdA()
    Vw.wda.show()


class WdA(QWidget):

    def __init__(self):
        super().__init__()
        self.resize(300, 50)
        self.setContentsMargins(0, 0, 0, 0)

        self.grid = QGridLayout(self)
        self.setLayout(self.grid)

        self.el1 = QLabel("About to clear!", self)
        self.grid.addWidget(self.el1, 0, 0)

        self.wOk = QPushButton("Confirm clearing", self)
        self.wOk.setFixedSize(120, 30)
        self.grid.addWidget(self.wOk, 1, 0)
        self.wOk.setFocus()
        self.wOk.clicked.connect(self.end_of_entry)
        self.wOk.setAutoDefault(True)

    def end_of_entry(self):
        self.close()
        # Vw.clearing()         # <--- This line needs to be implemented


def appl():
    app_ = QApplication(sys.argv)
    wnd = Vw()
    wnd.show()
    sys.exit(app_.exec())


if __name__ == '__main__':
    appl()

In other words, I would like 'clearing' method would trigger on clicking on 'wOk' button.

In other words, I would like 'clearing' method would trigger on clicking on 'wOk' button.


Solution

  • As an option using Signals & Slots.

    Support for Signals and Slots

    import sys
    '''
    from PyQt6.QtWidgets import (
        QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout
    )
    '''
    from PyQt5.QtWidgets import (
        QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout
    )
    from PyQt5.QtCore import pyqtSignal                                       # <--- 
    
    
    class WdA(QWidget):
        signal = pyqtSignal()                                                 # <--- 
        
        def __init__(self):
            super().__init__()
            self.resize(300, 50)
            self.setContentsMargins(0, 0, 0, 0)
    
            self.grid = QGridLayout(self)
            self.setLayout(self.grid)
    
            self.el1 = QLabel("About to clear!", self)
            self.grid.addWidget(self.el1, 0, 0)
    
            self.wOk = QPushButton("Confirm clearing", self)
            self.wOk.setFixedSize(120, 30)
            self.grid.addWidget(self.wOk, 1, 0)
            self.wOk.setFocus()
            self.wOk.clicked.connect(self.end_of_entry)
            self.wOk.setAutoDefault(True)
    
        def end_of_entry(self):
            self.signal.emit()                                                 # <--- 
            self.close()
    
    
    class Vw(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(1000, 400)
            self.setContentsMargins(30, 30, 30, 30)
    
            self.grid = QGridLayout(self)
            self.setLayout(self.grid)
    
            self.t1 = QLineEdit(self)
            self.grid.addWidget(self.t1, 0, 1)
    
            self.bOk = QPushButton("Clear the line edit", self)
            self.bOk.setFixedSize(120, 30)
            self.grid.addWidget(self.bOk, 5, 0)
            self.bOk.clicked.connect(self.implementing)
            self.bOk.setAutoDefault(True)
            
            self.wda = WdA()                                                  # +++
            self.wda.signal.connect(self.clearing)                            # <--- 
    
        def implementing(self):
            self.wda.show()                                     
    
        def clearing(self):                                                   # <---
            self.t1.clear()
            self.t1.setFocus()
            
        def closeEvent(self, event):    
            self.wda.close()
            
    
    def appl():
        app_ = QApplication(sys.argv)
        wnd = Vw()
        wnd.show()
        sys.exit(app_.exec())
    
    
    if __name__ == '__main__':
        appl()
    

    enter image description here

    enter image description here