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)

Morix Dev
  • 2,700
  • 1
  • 28
  • 49
user3891367
  • 55
  • 2
  • 8

1 Answers1

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.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • 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 Jul 30 '14 at 13:33
  • @user3891367, hm, are you sure you use the same code as above? – vahancho Jul 30 '14 at 13:38
  • @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 – Morix Dev Aug 27 '14 at 10:03