Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • I have created a set of Qline edits successfully and assigned each line edit 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 QLine edits!

    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?


  • Moderators

    @Lasith said in Getting text values from dynamically created Qline edits in Qt c++:

    getList<<(ui->("Textbox"+oName)->text());

    That part is wrong. The objectName property is a property of a C++ object. It is not name of the C++ variable, and not part of ui.

    In order to get a list of QObjects with certain objectName, you should use findChild<>() method, or findChildren<>().

    Side note: in each step of your first for loop, you create 3 new QLineEdits, but you only change the objectName of one of them (b). The rest remains unnamed.


Log in to reply