Question

I am trying to make a simple app with PySide. I have a grid layout with some cells being QLineEdit widgets. I want to clear the text fields of the 4 of these by a button click. Here is a part of my code:

editFields = [angle1Edit, angle2Edit, len1Edit, len2Edit]

clearBtn.clicked.connect(self.clearAll(editFields))

def clearAll(self, fields):
    for field in fields:
        return field.clear

In the editFields I collected the 4 widgets which I want to clean. But this only clears the first one but not all of them.

How can I do it for all? Is there another possibility to perform such action? Maybe I can use other widget for this task? Thank you.

Was it helpful?

Solution

First of all, I think you want field.clear() as field.clear will just give the clear function. Secondly, QLineEdit.clear() doesn't return anything so just:

for field in fields:
    field.clear()

Is needed as opposed to

for field in fields:
    return field.clear()

Lastly, QAbstractButton.clicked is probably trying to pass the checked argument which is causing issues, and might be forcing it to get the editFields[false] field, aka the editFields[0] field which is the first.

So, in conclusion, I'd make the editFields belong to what ever holds the fields and button, and try this and see if it gives the results you need:

self.editFields = [angle1Edit, angle2Edit, len1Edit, len2Edit]

clearBtn.clicked.connect(self.clearAll)

def clearAll(self, checked=False):
    for field in self.editFields:
        field.clear()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top