Dynamically adding QLineEdits and then copy text from them to vector in right order Here is how it looks at the beginning:



And after I can add random counts of line edits:



Or delete them:



The main purpose is the possibility to add or delete QlineEdits which stores user names and each QlineEdit adds a username to the vector in the right order (index) like this:

users =&gt;
 [0] = "a";
 [1] = "b";
 [2] = "c";
 [3] = "d";  
 [4] = "e";
 [5] = "f";
 [6] = "g";


Here is the code :
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QVBoxLayout&gt;
#include &lt;QHBoxLayout&gt;
#include &lt;QLineEdit&gt;
#include &lt;QPushButton&gt;
#include &lt;QList&gt;
#include &lt;vector&gt;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void addbutton_Clicked();
    void deletebutton_Clicked();
    void lineedit_textChanged(QString text);

private:
    QVBoxLayout *middlelayout;
    QList&lt;QLineEdit*&gt; qlist;
    std::vector&lt;QString&gt; users;
};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QWidget window = new QWidget(this);
    QVBoxLayout *mainlayout = new QVBoxLayout;
    middlelayout = new QVBoxLayout;
    QHBoxLayout *buttonlayout = new QHBoxLayout;
    QPushButton *addbutton = new QPushButton;
    QPushButton *deletebutton = new QPushButton;

    addbutton-&gt;setText("ADD");
    deletebutton-&gt;setText("Delete");

    buttonlayout-&gt;addWidget(addbutton);
    buttonlayout-&gt;addWidget(deletebutton);
    buttonlayout-&gt;setAlignment(Qt::AlignTop);
    mainlayout-&gt;addLayout(buttonlayout);
    mainlayout-&gt;addLayout(middlelayout);
    window-&gt;setLayout(mainlayout);
    setCentralWidget(window);

    connect(addbutton, SIGNAL(clicked()), SLOT(addbutton_Clicked()));
    connect(deletebutton, SIGNAL(clicked()), SLOT(deletebutton_Clicked()));
}

MainWindow::~MainWindow()
{
}

void MainWindow::addbutton_Clicked()
{
    QLineEdit *lineedit = new QLineEdit;
    qlist.push_back(lineedit);
    middlelayout-&gt;addWidget(lineedit);
    lineedit-&gt;setObjectName("lineedit"+QString::number(qlist.count()));

    users.resize(qlist.count());
    connect(lineedit, SIGNAL(textChanged(QString)), SLOT(lineedit_textChanged(QString)));
}

void MainWindow::deletebutton_Clicked()
{
    QWidget *child;
    if(!middlelayout-&gt;isEmpty())
        if((child = middlelayout-&gt;takeAt(qlist.count()-1)-&gt;widget()) != 0) {
            delete child;
            qlist.removeLast();
    }
}

void MainWindow::lineedit_textChanged(QString text)
{
    unsigned int index = 0;
    QObject *obj = QObject::sender();
    QString senderobjName = obj-&gt;objectName();
    int isTextchanged = senderobjName.indexOf("lineedit") != -1;

    if(isTextchanged)
    {
        QStringList list = senderobjName.split("lineedit");
        index = list[1].toInt();
        users[index]=text;
    }
}


The code works fine but it seems that I'm doing it wrong and there is a better way.
Since the MainWindow destructor is empty, you can just omit it.

In lineedit_textChanged, you declare the index variable at the top,
but it's only used if the text has changed.
It would be better to declare it inside the if branch where you actually use it:

if (isTextchanged)
{
    QStringList list = senderobjName.split("lineedit");
    unsigned int index = list[1].toInt();
    users[index] = text;
}


This snippet can be a bit confusing without braces on the outer if:


if(!middlelayout-&gt;isEmpty())
    if((child = middlelayout-&gt;takeAt(qlist.count()-1)-&gt;widget()) != 0) {
        delete child;
        qlist.removeLast();
}



It would be clearer this way:

if (!middlelayout-&gt;isEmpty()) {
    if ((child = middlelayout-&gt;takeAt(qlist.count()-1)-&gt;widget()) != 0) {
        delete child;
        qlist.removeLast();
    }
}