4

My requirement: dynamically create check boxes based on the no.of lines in a text file loaded.

Below is my code :

QVBoxLayout *lay = new QVBoxLayout(this);
for(i=0;i<number_of_commands;i++)
{
    QCheckBox *dynamic = new QCheckBox(names[i]);
    dynamic->setChecked (true);
    lay->addWidget(dynamic);
}
ui->scrollAreaWidgetContents_2->setLayout(lay);

I can see the correct number of check boxes but only first box is named that too with last name (i.e. box1 is named with names[20] and all other boxes are empty)

1
  • hi all the code is working fine. Problem is with the names[i] array. Thanks all for the help. Commented Sep 5, 2014 at 9:00

1 Answer 1

5

Checkboxes have no names (associated texts) because you do not set them. You can set it on construction, for example:

QCheckBox *dynamic = new QCheckBox("This is a check box");

The setObjectName() function you use is QObject (the base class) function, and it sets rather the object name, that has a different meaning.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. after trying "new QCheckBox(names[i]);", i got 20 boxes but names are getting overridden for the first box. so names[20] is assigned to first box. remaining boxes are still empty. any help here?
@user3891367, hm, are you sure you use the same code as above?
@user3891367: I think you should accept vahancho's answer if it helped you... otherwise if you fixed your code in a different way and/or if the question has sense no more, then consider closing it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.