Search code examples
c++qtqtgui

How to use automatically added Qt-elements


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

Solution

  • 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.