Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.

    0_1544669854109_31ea1efe-7005-4d0e-8777-1ef93e5394c5-image.png

    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.


  • Moderators

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


  • @JKSH

    Is there any other way to add controls dynamically?

    0_1544713865141_0591a592-1abf-41bd-a246-44a8c6a52715-image.png

    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

    0_1544713960274_e45fc757-6c43-42ab-bfbb-ba45851ef501-image.png

    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.


  • Lifetime Qt Champion

    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

    alt text



  • @mrjj

    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


  • Qt Champions 2017

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


  • Lifetime Qt Champion

    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.


Log in to reply