python - 在布局内的小部件内循环小部件

标签 python pyqt qgroupbox

与我们这里的问题类似的问题 Loop over widgets in PyQt Layout但有点复杂...

我有

QVGridLayout
   QGroupBox
      QGridLayout
         QLineEdit

我想访问 QLineEdit 但到目前为止我不知道如何访问 QGroupBox 的 child
        for i in range(self.GridLayout.count()):
            item = self.GridLayout.itemAt(i)
            for i in range(item.count()):
                lay = item.itemAt(i)
                edit = lay.findChildren(QLineEdit)
                print edit.text()

any1可以指出我正确的方向吗?

最佳答案

当一个小部件被添加到布局时,它会自动成为设置它的布局的小部件的子级。所以这个例子简化为两行:

for group in self.GridLayout.parentWidget().findChildren(QGroupBox):
    for edit in group.findChildren(QLineEdit):
        # do stuff with edit

然而,findChildren是递归的,因此如果所有行编辑都在组框中,则可以将其简化为单行:
for edit in self.GridLayout.parentWidget().findChildren(QLineEdit):
    # do stuff with edit

关于python - 在布局内的小部件内循环小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39450141/

相关文章:

python - 使用 pandas 找到矩阵中的负对

python - 如何在 PyQt5 中的按钮上添加条件?

python - Pyqt:如何更改QGroupBox标题颜色?

python - PyQt:如何在子类 QWidget 中接收键盘事件?

python - 根据列表有条件地创建组框

c++ - QGroupBox 的 children().count() 返回值超过预期

python - wxPython或wxPython中的PyGame Canvas ->哪个更快?

python - 如何在Python Django 中更新文章?

python - 执行 f2py fib1.f -m fib2 -h fib1.pyf 时出现以下错误 File "^ SyntaxError : invalid syntax

python - 刷新 GUI 窗口