这是我的简单结构:
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(); }我不断收到:'class QWidget' 没有名为 'text' 的成员
我能做什么?谢谢!
最佳答案
最简单的方法是在实际的父部件上使用 QObject::findChildren()
方法。
const QList<QLineEdit*> lineEdits = ui->widgetThatHasSwitchesLayout_2->findChildren<QLineEdit*>();
for (QLineEdit *lineEdit : lineEdits) {
req += " " + lineEdit->text();
}
关于c++ - Qt 获取布局中所有 QLineEdits 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42846185/