简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM