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 => [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 <QMainWindow>#include <QVBoxLayout>#include <QHBoxLayout>#include <QLineEdit>#include <QPushButton>#include <QList>#include <vector>class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();private slots:    void addbutton_Clicked();    void deletebutton_Clicked();    void lineedit_textChanged(QString text);private:    QVBoxLayout *middlelayout;    QList<QLineEdit*> qlist;    std::vector<QString> users;};#endif // MAINWINDOW_Hmainwindow.cpp#include mainwindow.hMainWindow::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->setText(ADD);    deletebutton->setText(Delete);    buttonlayout->addWidget(addbutton);    buttonlayout->addWidget(deletebutton);    buttonlayout->setAlignment(Qt::AlignTop);    mainlayout->addLayout(buttonlayout);    mainlayout->addLayout(middlelayout);    window->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->addWidget(lineedit);    lineedit->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->isEmpty())        if((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {            delete child;            qlist.removeLast();    }}void MainWindow::lineedit_textChanged(QString text){    unsigned int index = 0;    QObject *obj = QObject::sender();    QString senderobjName = obj->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.
Dynamically adding QLineEdits and then copy text from them to vector in right order
c++;qt
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->isEmpty())    if((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {        delete child;        qlist.removeLast();}It would be clearer this way:if (!middlelayout->isEmpty()) {    if ((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {        delete child;        qlist.removeLast();    }}