I want to be able to remove the added LineEdit. enter image description here

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); } 

enter image description here

  • one
    I do not understand what the question is? In the title about the event, at the beginning of the text about the deletion, then some kind of a sheet of code and some screenshots. Ask a specific question. ZY do not call your custom widgets QWhatever, with Q begin the built-in widgets of the kyut. Confused. ZZY To add an id field to your lineedit you don’t need to subclass it, just use the setProperty - Bearded Beaver method
  • @Bearded Beaver I need to delete Line Edit which I choose. How to do it? - Ivan Triumphov
  • @IvanTriumphov, store in your container your line_edits, for example: an array, a list, a dictionary. When you delete, get the link from the container / pointing to the line_edit and call it deleteLater - gil9red
  • @ gil9red Problem in the events. I do not understand how to catch the event clicking on LineEdit. Described above is the problem. LineEdit is hidden if you pass FieldlineEdit-> installEventFilter (this) to them; If there is a specific solution you can write it? - Ivan Triumphov
  • In the sense of hiding? Try to installEventFilter and log all events, filtering first those that come by type (MouseButtonPress), and after that by the objects themselves - gil9red

0