1

我正在尝试使用 PySide 制作一个简单的应用程序。我有一个网格布局,其中一些单元格是QLineEdit小部件。我想通过单击按钮清除其中 4 个的文本字段。这是我的代码的一部分:

editFields = [angle1Edit, angle2Edit, len1Edit, len2Edit]

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

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

editFields我收集了我想要清理的 4 个小部件。但这只会清除第一个而不是全部。

我怎样才能做到这一点?是否有另一种可能性来执行此类操作?也许我可以使用其他小部件来完成这项任务?谢谢你。

4

1 回答 1

2

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()
于 2013-09-26T01:35:25.010 回答