如果我想从 python Qt 的布局中获取特定的小部件,我应该如何进行?
到目前为止我所做的:
for i in range(self.ui.horizontalLayout_14.count()):
#here it does fail
name = self.ui.horizontalLayout_14.itemAt(i).objectName()
#if the above would had worked, then I could do something like this for example
if "button" in name:
self.ui.horizontalLayout_14.itemAt(i).widget().close()
请注意,对于示例,我正在使用button
但它可能是布局、lineEdit 或组合框、标签等内的任何小部件,但不是全部。
问题是itemAt()
函数返回QLayoutItem
而不是一个小部件。所以你必须调用QLayoutItem::widget()
函数来获取包含的小部件,即:
name = self.ui.horizontalLayout_14.itemAt(i).widget().objectName()
UPDATE
但是,您可以使用以下命令更轻松地完成相同的操作QObject::findChild()
功能。 IE。:
widget = self.ui.findChild(QPushButton, "button")
# Check whether the returned widget is null...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)