Qt C ++ - accessing dynamically created widget (QLineEdit)

I am building an application where almost all UI elements are dynamically created ... Among them is a list of QLineEdit

+ pairs QPushButton

generated from the number entered by the user. The buttons open a window Dialog

for searching for files, QLineEdits

for checking / editing data, and all this should end in the database. Sometimes it only has to enter 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;

    warstwaDialog[i] = new QPushButton;
    warstwaDialog[i]->setText("Dodaj element");
    mapperDialog->setMapping(warstwaDialog[i], i); 
    connect(warstwaDialog[i], SIGNAL(clicked()), mapperDialog, SLOT(map()));


But I can't get Dialog to pass String to "my" shortcut. In the Dialog slot, I am trying to use



But apparently QLayout (

) (and scrollAreaWidgetContentsFormularzWarstw

) is a "warstwaEdit" member. qDebug()

used in this slot indicates that the correct one is being transmitted i

. TreeDump indicates what scrollAreaWidgetContentsFormularzWarstw

is the parent.

I was a little confused. I have a very strange app for my first encounter with Qt ...


source to share

1 answer

The syntax you are using to access the widget seems to be wrong.

Since you store your widgets in an array, you don't need to access them through the layout. Just access them directly in your array:



An alternative would be the name of your widgets:

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


Then to access them with find


QLineEdit* lineEdit = ui->scrollAreaWidgetContentsFormularzWarstw->findChild<QLineEdit*>("some name");




All Articles