【问题标题】:Qt get all QLineEdits' texts within a layoutQt 在布局中获取所有 QLineEdits 的文本
【发布时间】:2017-08-08 08:12:31
【问题描述】:

这是我的简单结构:

QVBoxLayout 称为switchesLayout_2 | |\_ QHBoxLayout | | | |\_ QL标签 | \_ QEditLine | |\_ QHBoxLayout | | | |\_ QL标签 | \_ QEditLine 等等...

我需要从switchLayout_2 中的每个QEditLine 中获取一个文本。 我试过这段代码:

for(int i = 0; i switchesLayout_2->itemAt(i)->layout()->itemAt(1)->widget()->text(); } 我不断得到:'class QWidget'没有名为'text'的成员

我能做什么?谢谢!

【问题讨论】:

  • 您可能想要使用QLineEdit* pLE = qojbect_cast<QLineEdit>(ui->switchesLayout_2->itemAt(i)->layout()->itemAt(1)->widget()); 来获取指向QLineEdit 的指针。然后,如果那不是 nullptr,则使用 pLE->text(); 获取 QString
  • 我不断收到:'class QWidget' 没有名为 'text' 的成员 编译器是正确的 QWidget 没有 text() 成员。
  • 它抛出'cannot convert qlineedit to qlineedit in initialization
  • 好的,它有效。必须在演员阵容中将 * 添加到 QLineEdit。我需要以某种方式释放内存吗?
  • 我是否需要以某种方式释放内存不。

标签: c++ qt layout


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多