Clean LineEdits and Labels when closing Pyside sub window

0

I have a program with an interface made with Pyside. It is very simple and consists of 2 windows, the main (1st) and a secondary (2nd) with a lot of lines edits and labels to fill. What I would like to know is if there is a way so that when you close the 2nd window all the fields are deleted and when you open it again all the empty fields are empty.

It may be worth cleaning when closing or opening again.

I know that it can be done by putting all the blank edits and labels one by one at the start, but it was in case there is a more automatic and fast way.

Greetings and thanks

    
asked by Tercuato 09.01.2018 в 12:56
source

1 answer

0

The widgets that appear in the window are children of the window, or children of their children, taking advantage of that property we can find them all through the findChildren() , we must pass as a parameter the name of the widget class.

Example:

import random
from PySide.QtGui import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        grid = QGridLayout(self)

        l = [QLineEdit, QLabel]

        for i in range(4):
            for j in range(3):
                w = random.choice(l)("sometext")
                grid.addWidget(w, i, j)

        clearBtn = QPushButton("Clear")
        grid.addWidget(clearBtn, 5, 0, 3, 3)
        clearBtn.clicked.connect(self.clearAll)

    def clearAll(self):
        for le in self.findChildren(QLineEdit):
            le.clear()
        for lbl in self.findChildren(QLabel):
            lbl.setText("")


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
    
answered by 09.01.2018 в 16:30