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;
    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 Dialog to pass String to "my" shortcut. In the Dialog slot, I am trying to use

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

      

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

+3


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:

warstwaEdit[i]->setText(filepath);

      

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");
lineEdit->setText(filePath);

      

+6


source







All Articles