QLineEdit Qt in c++ removing QLineEdit

500 views Asked by At

I'm trying to make an adress book in Qt and i am using the following code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

int counter = 1;

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QLineEdit* Voornaam = new QLineEdit(this);
    Voornaam->setObjectName(QString::fromUtf8("lineEdit_4"));
    Voornaam->setGeometry(QRect(10, 65+ 33*counter, 113, 24));
    Voornaam->show();

    QLineEdit* Achternaam = new QLineEdit(this);
    Achternaam->setObjectName(QString::fromUtf8("lineEdit_5"));
    Achternaam->setGeometry(QRect(140, 65+ 33*counter, 113, 24));
    Achternaam->show();

    QLineEdit* Adres = new QLineEdit(this);
    Adres->setObjectName(QString::fromUtf8("lineEdit_6"));
    Adres->setGeometry(QRect(270, 65+ 33*counter, 113, 24));
    Adres->show();

    counter+= 1;
}

void MainWindow::on_pushButton_2_clicked()
{
}

As you can see in the second function the pushbutton_2_clicked i haven't put anything yet. The program works like this: it adds a QLineEdit every time I push a button(the first function). Then it displays it on the ui. Also I just make a new QLineEdit everytime and move it a bit down so I get a list of these. Voornaam, achternaam and adress are just names for information I want to type in these QLineEdits in my program.

What I want to do now is delete these QLineEdit but i don't know how, I've searched this on the internet but i can't find examples. So I want to delete these previously made QLineEdits. Do I have to use the name ? LineEdit_4 for example. I've found a widget function removewidget, do I use this?

Is there a way to easier display these widgets? Now I'm just making rectangles and placing them beneath eachother using the counter.

1

There are 1 answers

1
p.i.g. On

Why don't you use for example a QTableWidget. You can add/remove rows from that (and columns if you like). And you use setCellWidget function to add QLineEdits into the cells.

You could have a button to add a new line. And you could have a button to delete the selected lines from the table.