我想点击一个按钮并清除大约20个QLineEdits。
我正在尝试使用findChildren()并将所有QLineEdit放入QListWidget
self.StudentAdmissionLayout = QGridLayout()
self.StudentAdmissionLayout.addWidget(self.StudentName,1,0,1,1)
self.StudentAdmissionLayout布局上放置了所有QLineEdit。
self.myList = QListWidget()
self.Item = QListWidgetItem()
self.Item = self.StudentAdmissionLayout.findChildren(QLineEdit)
self.myList.addItem(self.Item)
我收到以下错误:
TypeError: arguments did not match any overloaded call:
QListWidget.addItem(QListWidgetItem): argument 1 has unexpected type 'list'
QListWidget.addItem(QString): argument 1 has unexpected type 'list'
我试图把上面4行放在一个循环中。但第3行不起作用,我不知道如何使它工作。 请建议。
答案 0 :(得分:4)
好的,我们必须了解list-of-QObject QObject.findChildren (self, type type, QString name = QString())
的行为。我参考了这个课,它说;
返回具有给定名称的此对象的所有子项 如果没有这样的对象,则转换为类型T或空列表。 省略name参数会导致所有对象名称都匹配。该 搜索是递归执行的。
参考:http://pyqt.sourceforge.net/Docs/PyQt4/qobject.html#findChildren
然后,它返回python list
给你,列表中的元素是类型T(键入你的输入)。所以你有问题;
TypeError: arguments did not match any overloaded call: QListWidget.addItem(QListWidgetItem): argument 1 has unexpected type 'list' QListWidget.addItem(QString): argument 1 has unexpected type 'list'
该错误,因为您列出了部分列表,但实际上它只能是QListWidgetItem
或QString
(或python string
)。如果你想分开它,你必须使用" for loop"迭代数据;
myQListWidget = QListWidget()
listsItem = ['my', 'data', 'is', 'here', '!']
for item in listsItem:
myQListWidget.addItem(item)
我发现的另一个问题是,您搜索的数据是QLineEdit
,它不支持QListWidget.addItem()
重载方法的类型。我认为你不能参与这个方法。但如果你需要" text"仅在QLineEdit
。你可以做到这一点;
self.studentAdmissionQGridLayout = QGridLayout()
.
.
.
self.myQListWidget = QListWidget()
listsMyQLineEdit = self.studentAdmissionQGridLayout.findChildren(QLineEdit)
for myQLineEdit in listsMyQLineEdit:
self.myQListWidget.addItem(myQLineEdit.text())
此参考了解QListWidget.addItem()
。
参考:http://pyqt.sourceforge.net/Docs/PyQt4/qlistwidget.html#addItem
此致
答案 1 :(得分:1)
如果行修改没有将布局设为父级,findChildren
将无法找到它们。
迭代这样的布局可能更好:
for row in range(layout.rowCount()):
for column in range(layout.columnCount()):
item = layout.itemAtPosition(row, column)
if item is not None:
widget = item.widget()
if isinstance(widget, QLineEdit):
listWidget.addItem(widget.text())
答案 2 :(得分:1)
所以我最终希望能够循环访问已加载的uic模块中的所有子对象,并动态链接它们,而此循环可能对您有帮助,仅对qlineedit进行了一些轻微的修改
for name, obj in dict(self.__dict__).items():
# print(str(name) + str(obj))
obj_type = str(obj).strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
# obj_type = str(obj).strip("<").rsplit(" ")[0]
# print(obj_type)
# obj_type = obj_str.strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
label_name = "self." + str(name)
try:
label_name = self.findChild(eval(obj_type), name)
print(str(label_name) + ' created')
except:
pass
if not isinstance(obj_type, QObject):
continue
下面是我能够加载Qt Designer uic文件并连接自定义插槽和信号以及实时测试的存根的完整代码
from PyQt5 import uic, QtWidgets
import sys
from PyQt5.QtCore import QObject
qtCreatorFile = "mainwindow.ui" # Type your file path
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class build(Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
for name, obj in dict(self.__dict__).items():
# print(str(name) + str(obj))
obj_type = str(obj).strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
# obj_type = str(obj).strip("<").rsplit(" ")[0]
# print(obj_type)
# obj_type = obj_str.strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
label_name = "self." + str(name)
try:
label_name = self.findChild(eval(obj_type), name)
print(str(label_name) + ' created')
except:
pass
if not isinstance(obj_type, QObject):
continue
def start():
app = QtWidgets.QApplication(sys.argv)
bld = build()
bld.show()
sys.exit(app.exec_())
if __name__ == '__main__':
start()