簡體   English   中英

Qt在布局中獲取所有QLineEdits的文本

[英]Qt get all QLineEdits' texts within a layout

這是我的簡單結構:

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

我需要從switchsLayout_2中的每個QEditLine獲取文本。 我已經試過這段代碼:

for(int i = 0; i < switchesAmount; i++) {
        req += " " + ui->switchesLayout_2->itemAt(i)->layout()->itemAt(1)->widget()->text();
    }
我不斷得到:“ QWidget類”沒有名為“ text”的成員

我能做什么? 謝謝!

最簡單的方法是在實際的父窗口小部件上使用QObject::findChildren()方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM