Hi, I need help with dynamic controls.
-
I want to add 3 controls dynamically to a QWidget, I'm trying with QLabel but it does not work as expected, I really do not know how to do it, look for information but it did not help me at all.
I would like to add them as we see the two QLabel with the QChekcBox above.
And go through them to see if any checkbox was clicked, I do not know if I should save them so QList <QCheckBox *> to know if it was clicked?Thank you very much for your answers.
-
@Allan5 Your widget (widget_controles) already has a layout. That's why you cannot add new layout to it.
Instead, add your new widget to the existing layout:
auto existingLayout = ui->widget_controles->layout(); existingLayout->addWidget(new QLabel("Campo"));
@Allan5 said in Hi, I need help with dynamic controls.:
And go through them to see if any checkbox was clicked, I do not know if I should save them so QList <QCheckBox *> to know if it was clicked?
Call the
isChecked()
function to find out if it was clicked.bool c = ui->checkbox->isChecked();
-
Is there any other way to add controls dynamically?
I also want to add a QCheckbox because I plan to do something similar to this, but I could not get it to add controls dynamically and go through each QCheckBox to see if it was clicked.
Thanks for your response
In Workbench I can import fields from a csv file, in a GroupBox it shows the columns that csv brings, I want to do that. But to do it I need to create controls dynamically.
-
Hi
are you sure capa is not NULL ?
seems to crash so i guess you did not apply a layout in Designer ?
Also even the layout is inside a Widget, the reference from UI is
kinda flat.
like
ui->verticalLayout->addWidget( new QCheckBox("test") );small sample
https://www.dropbox.com/s/1t1hll6gt32usp9/DynamicCheckBoxes.zip?dl=0 -
Thank you for your example project, now the question, to go through each new control that I add, can I do this?
QList <QCheckBox *> listCheckBox;
listCheckBox.append (newCheckBox);It will be that way I will be able to detect if some checkbox was clicked and it is true?
Because when you press a button you have to go through the controls and look for the ones that were clicked.
Thanks for your help
-
@Allan5 said in Hi, I need help with dynamic controls.:
QList <QCheckBox *> listCheckBox;
listCheckBox.append (newCheckBox);You can do this. Hope you are doing signal/slot connection for each checkbox as well.
-
Hi
Alternatively to keeping your own list.
QList<QCheckBox*> AllChecks = ui->groupBox->findChildren<QCheckBox*>();
Just make sure that
ui->groupBox matches your parent. That is - the widget that has the checkboxes.