c++ - Qt 获取布局中所有 QLineEdits 的文本

标签 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();
    }
我不断收到:'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/

相关文章:

c++ - 删除控制台/cmd 提示符上闪烁的下划线

qt - 如何正确地将值从pyqt返回到JavaScript?

java - 更改 Swing 组件的首选大小

css - 返回顶部按钮 - 根据分辨率更改固定位置

c++ - OpenGL 4 : Draw with glVertexAttrib

c++ - 找不到 -lboost_system 的库

c++ - 如何实现高效的C++运行时统计

c++ - QNetworkReply::error 信号什么时候会跟着一个 finish() 信号?

qt - 如何在Qt中获取应用程序字体颜色

css - 像旧书一样创造伟大的大写字母