QHBoxLayout QFormLayout 遍历子部件,查找QLineEdit控件

Posted azbane

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
                                                                                                                                  

 

以上是关于QHBoxLayout QFormLayout 遍历子部件,查找QLineEdit控件的主要内容,如果未能解决你的问题,请参考以下文章

Qt-demo-basiclayouts

pyqt

Qt布局与分割器QSplitter

PyQt5 笔记(01):嵌套布局

Python Qt GUI设计:窗口布局管理方法强化篇(基础篇—6)

3.1.3 QFormLayout表单布局(窗体布局)