How to use automatically added Qt-elements

531 views Asked by At

My programm can add new QLabels and QLineEdits to a QScrollArea after a button is clicked. The idea is to create a grocery list. My problem is when a second Button is clicked I want to get the text of all the QLineEdits. But I don't know how to use those elements, because every new QLineEdit-variable has the same name and I don't know how to change that.

Below is a small example:

my MainWindow.h:

#ifndef MainWINDOW_H
#define MainWINDOW_H

#include <QMainWindow>
#include <string> 

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    private:
        Ui::MainWindow *ui;

private slots:
    void on_create_clicked();
    read_text();
};

#endif // MainWINDOW_H

my MainWindow.cpp:

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_create_clicked()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(read_text()));
    i = 1;
}

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

void MainWindow::on_create_clicked()
{
    if(i < 10)
    {
        i ++;
        QLabel *label_2 = new QLabel();
        QString s =  QString::number(zaehlerHeight) + ". ";
        label_2->setText(s);
        ui->scrollArea->widget()->layout()->addWidget(label_2);

        QLineEdit *lineEdit = new QLineEdit();
        ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
    }
    else{
        ui->label->setText("already 10");
    }
}

void MainWindow::read_text()
{
    QString mytext = ui->lineEdit->text();
}
3

There are 3 answers

0
Jean-Emmanuel On BEST ANSWER

I would simply store the pointer to each QLineEdit in a QVector, and then loop in this vector to get the text of each.

Header:

#ifndef MainWINDOW_H
#define MainWINDOW_H

#include <QMainWindow>
#include <string> 
#include <QVector>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    private:
        Ui::MainWindow *ui;
        QVector<QLineEdit *> m_VecLineEdits;

private slots:
    void on_create_clicked();

private:
    void read_text();
    void GetAllTextEdit();
};

#endif // MainWINDOW_H

In Cpp file, change the following:

void MainWindow::on_create_clicked()
{
    if(i < 10)
    {
        i ++;
        QLabel *label_2 = new QLabel();
        QString s =  QString::number(zaehlerHeight) + ". ";
        label_2->setText(s);
        ui->scrollArea->widget()->layout()->addWidget(label_2);

        QLineEdit *lineEdit = new QLineEdit();
        m_VecLineEdits.push_back(lineEdit); // <-- Line added here to save the pointers in a QVector.
        ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
    }
    else{
        ui->label->setText("already 10");
    }
}

void MainWindow::GetAllTextEdit()
{
    for(int j = 0; j<m_VecLineEdits.size(); ++j)
    {
        QString lineEditText = m_VecLineEdits.at(j)->text();
        /* Do anything with this value */
    }
}

If you delete your QLineEdit, remember to also remove them from the QVector.

0
kzsnyk On

If you want to change the name of the variable ( ie the pointer to the QLineEdit ) each time you slot is called, and provided that i will stay small ( < 10 ), you can use switch(i) for example and choose a different variable name for each case, but you will have to store all those variables as members of your class. So it's better to store the pointers in a QList or a QVector and loop over those containers to access the text() method on each QLineEdit.

0
Mihai On

you can't, because you don't have any pointer or reference to those objects, one solution would be having a array of QLabel in your class definition.

ex:

QVector<QLabel*> _labels;

and adding and instantiating one by one with the press of the button and then you will have the whole list of objects, thus their names