I want to be able to remove the added LineEdit. 
The code itself dynamic LineEdit:
qdynamiclineedit.h
#ifndef QDYNAMICLINEEDIT_H #define QDYNAMICLINEEDIT_H #include <QLineEdit> #include <QObject> #include <QWidget> class QDynamicLineEdit : public QLineEdit { Q_OBJECT public: explicit QDynamicLineEdit(QWidget *parent = 0); ~QDynamicLineEdit(); int getID(); static int ResID; private: int LineEditID = 0; }; #endif // QDYNAMICLINEEDIT_H qdynamiclineedit.cpp
#include "qdynamiclineedit.h" QDynamicLineEdit::QDynamicLineEdit(QWidget *parent): QLineEdit(parent) { ResID++; LineEditID = ResID; //QDynamicLineEdit::installEventFilter(parent); } QDynamicLineEdit::~QDynamicLineEdit() { } int QDynamicLineEdit::getID() { return LineEditID; } int QDynamicLineEdit::ResID = 0; The very code of the form and add a field to the log:
addlog.h
#ifndef ADDLOG_H #define ADDLOG_H #include <QDialog> #include "qdynamiclineedit.h" namespace Ui { class AddLog; } class AddLog : public QDialog { Q_OBJECT public: explicit AddLog(QWidget *parent = 0); ~AddLog(); private slots: void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); void on_lineEdit_returnPressed(); bool eventFilter(QObject *watched, QEvent *event); void SetNumberLineEdit(); private: Ui::AddLog *ui; QDynamicLineEdit* FieldLineEdit; }; #endif // ADDLOG_H addlog.cpp
#include "addlog.h" #include "ui_addlog.h" #include <QDebug> AddLog::AddLog(QWidget *parent) : QDialog(parent), ui(new Ui::AddLog) { ui->setupUi(this); ui->lineEdit->setText("введите_название_журнала"); } AddLog::~AddLog() { delete ui; } void AddLog::on_pushButton_2_clicked() { FieldLineEdit = new QDynamicLineEdit(this); FieldLineEdit->setText(QString("введите_название_поля_%1").arg(FieldLineEdit->getID())); ui->verticalLayout_2->addWidget(FieldLineEdit); //FieldLineEdit->installEventFilter(this); //connect(FieldLineEdit, SIGNAL(), this, SLOT(SetNumberLineEdit())); } bool AddLog::eventFilter(QObject *watched, QEvent *event) { if(watched == FieldLineEdit) { if(event->type() == QEvent::MouseButtonPress) { ui->lineEdit_2->setText(QString("%1").arg(FieldLineEdit->getID())); } } //return AddLog::eventFilter(watched, event); } void AddLog::SetNumberLineEdit() { ui->lineEdit_2->setText(QString("%1").arg(FieldLineEdit->getID())); } void AddLog::on_pushButton_3_clicked() { } void AddLog::on_lineEdit_returnPressed() { qDebug()<<"1"; } The definition of the click event on LineEdit, which I tried to do. Just hides all the LineEdite except the last one added:
... QDynamicLineEdit::installEventFilter(parent); ... bool AddLog::eventFilter(QObject *watched, QEvent *event) { if(watched == FieldLineEdit) { if(event->type() == QEvent::MouseButtonPress) { ui->lineEdit_2->setText(QString("%1").arg(FieldLineEdit->getID())); } } //return AddLog::eventFilter(watched, event); } 