Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt C++ - access a dynamically created Widget (QLineEdit)

Tags:

c++

widget

qt

I'm making an app where practically all UI elements are dynamically created... Among them is a list of QLineEdit + QPushButton pairs generated from a number the user inputs. Buttons open up a Dialog window to find files, QLineEdits is for data verification/editing, and all that has to end up in a database. Sometimes he only has to input 3 values, sometimes 10.

QLineEdit* warstwaEdit[iloscWarstw]; //iloscWarstw - number user inputs
QPushButton* warstwaDialog[iloscWarstw];
for(int i=0; i<iloscWarstw; i++) {
    warstwaEdit[i] = new QLineEdit;
    warstwaEdit[i]->setFixedHeight(25);
    warstwaEdit[i]->setFixedWidth(400);
    ui->scrollAreaWidgetContentsFormularzWarstw->layout()->addWidget(warstwaEdit[i]);

    warstwaDialog[i] = new QPushButton;
    warstwaDialog[i]->setFixedWidth(100);
    warstwaDialog[i]->setFixedHeight(30);
    warstwaDialog[i]->setText("Dodaj element");
    ui->scrollAreaWidgetContentsFormularzWarstw->layout()->addWidget(warstwaDialog[i]);
    mapperDialog->setMapping(warstwaDialog[i], i); 
    connect(warstwaDialog[i], SIGNAL(clicked()), mapperDialog, SLOT(map()));
}

But I can't get the Dialog to pass the String to "his" label. In Dialog's slot I'm trying to use the

ui->scrollAreaWidgetContentsFormularzWarstw->layout()->warstwaEdit[i]->setText(filepath);

But apparently QLayout () (nor scrollAreaWidgetContentsFormularzWarstw) have a member "warstwaEdit". qDebug() used in that slot indicates that a proper i is being passed. TreeDump indicates that ScrollAreaWidgetContentsFormularzWarstw is the parent.

I'm kind of at a loss. I got a really weird app for my first encounter with Qt...

like image 962
Petersaber Avatar asked Mar 17 '23 07:03

Petersaber


1 Answers

The syntax you're using to access the widget doesn't seem correct.

Since you are storing your widgets in an array, you don't need to access them via the layout actually. Just access them directly on your array:

warstwaEdit[i]->setText(filepath);

An alternative would be to name your widgets:

warstwaEdit[i]->setObjectName("some name");

Then to access them with find:

QLineEdit* lineEdit = ui->scrollAreaWidgetContentsFormularzWarstw->findChild<QLineEdit*>("some name");
lineEdit->setText(filePath);
like image 66
laurent Avatar answered Mar 25 '23 05:03

laurent