Qt获得所有QLineEdits'布局中的文本

时间:2017-03-16 22:49:29

标签: c++ qt layout

这是我的简单结构:

QVBoxLayout called switchesLayout_2
 |
 |\_ QHBoxLayout
 |    |
 |    |\_ QLabel
 |     \_ QEditLine
 |
 |\_ QHBoxLayout
 |    |
 |    |\_ QLabel
 |     \_ QEditLine
and so on...

我需要从switchesLayout_2中的每个QEditLine获取一个文本。 我已尝试过此代码:

    for(int i = 0; i < switchesAmount; i++) {
        req += " " + ui->switchesLayout_2->itemAt(i)->layout()->itemAt(1)->widget()->text();
    }
我不断得到:&#39;班级QWidget&#39;没有名为&#39; text&#39;

的成员

我该怎么办?谢谢!

1 个答案:

答案 0 :(得分:1)

最简单的方法是在实际的父窗口小部件上使用QObject::findChildren()方法。

const QList<QLineEdit*> lineEdits = ui->widgetThatHasSwitchesLayout_2->findChildren<QLineEdit*>();
for (QLineEdit *lineEdit : lineEdits) {
    req += " " + lineEdit->text();
}