Important: Please read the Qt Code of Conduct -

Values of dynamically created QLineEdit

  • Hi everyone.

    I want to get text values from QLineEdit which it have been added dynamically.

    I have a loop for adding these widgets to tempLayout.

    tempLayout->addWidget(new QLabel(j.key()));
    tempLayout->addWidget(new QLineEdit(j.value()));  

    So how I can access the values that I want from the QLineEdit? I have about 4-5 labels and 6-7 of Qlineedits.

    I have tried this but it crashes when I access it.

    int children = tempLayout->count();
    if(children > 0)
        qDebug() << "Children count = "<< children;
        QString str = tr("");
        for(int i = 0; i < children; i++)
            QLayoutItem* pLine = tempLayout->itemAt(i);
            pLineEdit = (QLineEdit*)pLine->widget();
           qDebug() << "Children of templayout" << pLineEdit->text();
        qDebug() << "Values " << tempLayout->count();
        qDebug() << "No children found";

  • Lifetime Qt Champion

    Have you tried with

    QList<QLineEdit*> widgets = tempLayoutParent->findChildren<QLineEdit*>();

  • another way is set the objectName() of each QLineEdit and retrieve them by name

  • @Tirolel
    If you want t do it that way you shouldn't you cast the widget to find out what type it is before you actually use it?

    if(qobject_cast<QLineEdit*>(pLine->widget()) != 0)

    Test for which type it is then use the correct type cast to use the actual member functions...

Log in to reply