QHBoxLayout 、QFormLayout 遍历子部件,查找QLineEdit控件
布局如下: QLineEdit * edit1 = new QLineEdit; QLineEdit * edit2 = new QLineEdit; QLineEdit * edit3 = new QLineEdit; QLineEdit * edit4 = new QLineEdit; QFormLayout * formLayout1 = new QFormLayout; QFormLayout * formLayout2 = new QFormLayout; formLayout1->addRow(QStringLiteral("编辑1"), edit1); formLayout1->addRow(QStringLiteral("编辑2"), edit2); formLayout2->addRow(QStringLiteral("编辑3"), edit3); formLayout2->addRow(QStringLiteral("编辑4"), edit4); QHBoxLayout * hLayout = new QHBoxLayout; hLayout->addLayout(formLayout1); hLayout->addLayout(formLayout2); ... 遍历查找每一个布局,判断QLineEdit输入是否为空 foreach(auto formLayoutChild, hLayout->children()) { QFormLayout * formLayout = qobject_cast<QFormLayout*>(formLayoutChild); if (formLayout != nullptr) { int itemCount = formLayout->count(); for(int itemIndex = 0; itemIndex < itemCount; itemIndex++) { QLayoutItem * item = formLayout->itemAt(itemIndex); QLineEdit * lineEdit = qobject_cast<QLineEdit*>(item->widget()); if (lineEdit != nullptr) { if (lineEdit->text().isEmpty()) { return false; } } } } } return true;