Search code examples
c++qtdynamicgettextqlineedit

Getting text values from dynamically created Qline edits in Qt c++


I have created a set of QlineEdits successfully and assigned each LineEdit an Object name but unfortunately when I try to read and get them into a QStringList I get an error stating:

"Textbox was not declared in this scope"

my code is as follows:

for(int i=0;i<5;i++){
   f1 = new QFrame();
   f2 = new QFrame();
   f3 = new QFrame();

  a= new QLabel(f1);
  b=new QLineEdit(f2);
  c=new QLineEdit(f3);

    QString oName= QString::number(i);
    b->setObjectName("Textbox"+oName);

    ui->verticalLayout->addWidget(f1);
    ui->verticalLayout_2->addWidget(f2);
    ui->verticalLayout_3->addWidget(f3);

    a->setText(newList[i]);

}

and from the button click event I won't to get each text in the dynamically created QLineEdits!

void NewOrders::on_pushButton_2_clicked()
{

 for(int i=0;i<getList.size();i++){
       QString oName= QString::number(i);
     getList<<(ui->("Textbox"+oName)->text());
 }
}

Here getlist and newlist are QStirngLists are already defined as public! How can I correct this?


Solution

  • To get the object through the objectName you must use the findChild

    void NewOrders::on_pushButton_2_clicked()
    {
        for(int i=0;i<5;i++){
          QLineEdit *le = findChild<QLineEdit*>(QString("Textbox%1").arg(i));
          if(le){
              getList<<le->text();
          }
        }
    }