Search code examples
qtdynamicqlineedit

Qt Acces to dynamic qt LineEdit


I am programming a configurator. If the Addbutton is clicked, it adds several Labels and LineEdits dynamicly (See the Code below).

My Problem is following:

After I create the Labels and LineEdits they will be shown, but if the user changes the value of a LineEdits, how do I get access to that data? And more important how do I know which (the first, second, third) LineEdits or Label it is?

In the following code the values are not saved in lists, but I will add that as following step. I hope you get my question. The focus on my code should be on "onAbstandSent" there I create my LineEdit, on which I need acces later on. The important thing, as I mentioned is, that I have to know which LineEdit the user changed!

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    counter=0;

    ui->setupUi(this);

    //Verteiler links

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

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//SLOT zum Daten bekommen
void MainWindow::onDNSent (const QString & DN)
{
    DNhelp=DN;
    qDebug()<<(DNhelp);

    //showing DN label
    QLabel *DNlabel = new QLabel ();

  //  DNlabel->sets
    DNlabel->setParent(this);
    DNlabel->show();
    DNlabel->setText(DNhelp);
    //counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
    if  (counter==1)
    {
       DNlabel->setGeometry(xposition+80,507,40,40);
    }
    else
    {
       DNlabel->setGeometry(xposition+45,507,40,40);
    }
}

void MainWindow::onPNSent (const QString & PN)
{
    PNhelp=PN;
    qDebug()<<(PNhelp);

    //showing PN label
    QLabel *PNlabel = new QLabel ();

  //  DNlabel->sets
    PNlabel->setParent(this);
    PNlabel->show();
    PNlabel->setText(PNhelp);
    //counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
    if  (counter==1)
    {
       PNlabel->setGeometry(xposition+80,530,40,40);
    }
    else
    {
       PNlabel->setGeometry(xposition+45,530,40,40);
    }
}


void MainWindow::onAbstandSent (const double & mm)
{
    Abstand=mm;
    qDebug()<<(Abstand);
    //showing Abstand LineEdit
    QLineEdit *Abstandlineedit = new QLineEdit ();
    //can this help?
    ui->gridLayout->addWidget(Abstandlineedit,counter-1,0);
    Abstandlineedit->setParent(this);
    Abstandlineedit->show();
    Abstandlineedit->setText(QString::number(Abstand));
    //lineedit Hintergrund transparent machen
    Abstandlineedit->setStyleSheet("background:transparent;");
    //counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
    if  (counter==1)
    {
      Abstandlineedit->setGeometry(xposition+80,230,40,40);
    }
    else
    {
       Abstandlineedit->setGeometry(xposition+15,568,30,20);
    }

}
void MainWindow::on_pushButton_add_clicked()
{

    //label Verteiler Picture

    QPixmap vert_links("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_links");
    QPixmap vert_mitte("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_mitte");
    QPixmap vert_rechts("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_rechts");
    QLabel *label = new QLabel ();
    //add Label to the List


    xposition=counter*60;

    if  (counter==0)
    {
        label->setPixmap(vert_links);
    }
    else
    {
        xposition=xposition+35;
        label->setPixmap(vert_rechts);
    }
    if (counter>1)
    {
     qlist.last()->setPixmap(vert_mitte);

    }
    qlist.append(label);
    label->setParent(this);
    label->setGeometry((xposition),500,125,172);
    label->show();

    //opening choose dialog
     stu_info* stufo = new stu_info(this,counter);
     connect (stufo, &stu_info::sendDN,this,&MainWindow::onDNSent);
     connect (stufo, &stu_info::sendPN,this,&MainWindow::onPNSent);
     connect (stufo,&stu_info::sendAbstand,this,&MainWindow::onAbstandSent);
     stufo->setModal(true);
     stufo->show();



    counter++;

}

Solution

  • If I understand you correctly you want to know which lineedit was changed and not to change it programmaticaly.

    If so you can add some property to your lineedit, then connect your slot to the textChanged(const QString &) signal and in slot read your property. For example:

    Abstandlineedit->setProperty("id", someValue);
    connect(Abstandlineedit, SIGNAL(textChanged(const QString &)), this, SLOT(slotLineEdit(const QString&)));
    ....
    void MainWindow::slotLineEdit(const QString& s)
    {
      int id = sender()->property("id").toInt();
    //use id
    ...
    }
    

    But if you need to chage your lineedits somewhere in your code not as reaction on signal, then you for sure need to add some vector of pointers to this widgets and use it.