1

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.

4

1 に答える 1

2

まず、明確な機能を与えるだけでいいと思いfield.clear()ますfield.clear。次に、QLineEdit.clear()何も返さないので、次のようにします。

for field in fields:
    field.clear()

とは対照的に必要です

for field in fields:
    return field.clear()

最後に、問題を引き起こしQAbstractButton.clickedている引数を渡そうとしているchecked可能性があり、最初editFields[false]のフィールドとも呼ばれるフィールドの取得を強制している可能性があります。editFields[0]

したがって、結論として、フィールドとボタンを保持するものに editFields を所属させ、これを試して、必要な結果が得られるかどうかを確認します。

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

clearBtn.clicked.connect(self.clearAll)

def clearAll(self, checked=False):
    for field in self.editFields:
        field.clear()
于 2013-09-26T01:35:25.010 に答える