2

I generate checkboxes as follows:

foreach(QString filt, types){
    QCheckBox *checkbox = new QCheckBox(filt, this);
    checkbox->setChecked(true);
    vbox->addWidget(checkbox);
}

I need to get access to these checkboxes by name but they are all called the same?

I need to read the text they display.

How can I go about this?

Is it possible to run a for loop and attach the value of i onto the end of the checkbox. So in effect, the checkbox would be called checkbox[0], checkbox [1], etc?

EDIT:

I've changed the code to the following:

for(int i=0; i<types.count(); ++i)
{
    QString filt = types[i];
    *checkboxCount = *checkboxCount + 1;
    QCheckBox *typecheckbox[i] = new QCheckBox(filt, this);
    typecheckbox[i]->setChecked(true);
    vbox->addWidget(typecheckbox[i]);
}

I thought this was a way to dynamically name the checkboxes so I can loop through them to get the text value from them.

I'm getting the error 'variable-sized object may not be initialized' on this line QCheckBox *typecheckbox[i] = new QCheckBox(filt, this);

Any ideas to a solution/ alternate approach?

László Papp
  • 51,870
  • 39
  • 111
  • 135
sark9012
  • 5,485
  • 18
  • 61
  • 99

1 Answers1

2

If you want to access the checkboxes later, you can just use the find children method as follows:

QStringList myStringList;
QList<QCheckBox *> list = vbox->findChildren<QCheckBox *>();
foreach (QCheckBox *checkBox, list) {
    if (checkBox->isChecked())
        myStringList.append(checkBox->text());
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Perfect, I was hoping there was a way such as this. Using your method, how would I go about looping through the checkboxes and getting the text for each. The idea is that I want to pass it into a QStringList. – sark9012 Jul 21 '14 at 12:25
  • Text or actual object name which you do not seem to have set? – László Papp Jul 21 '14 at 12:42
  • QCheckBox *checkbox = new QCheckBox(filt, this); - I have the text set as 'filt' but the object name is just *checkbox for more than 1 checkbox! – sark9012 Jul 21 '14 at 12:55
  • Why do you care about the object name, especially if you do not set it? – László Papp Jul 21 '14 at 12:57
  • No, I'm trying to get the text! I set several checkboxes using the foreach loop I showed in the question. I then need to write some code that allows me to loop through those checkboxes and check if they are checked and if so, get the text from them and put it into a QStringList! – sark9012 Jul 21 '14 at 13:17
  • @Luke: it really was not in the original question, but I updated my answer. – László Papp Jul 21 '14 at 13:22
  • That is a fantastic answer. I wasn't expecting to get the code written for me like that, I was only trying to find a way to get the text from the checkbox and then work the rest out. Thankyou very much! – sark9012 Jul 21 '14 at 13:25
  • I've tried to incorporate this into my code but the foreach loop doesn't seem to be being hit. I've tried to print something from within the loop and nothing happens? – sark9012 Jul 21 '14 at 13:48
  • 1
    Well, you have to call `findChildren();` without `vbox->` if you parent to `this`, or you parent to `vbox`. – László Papp Jul 21 '14 at 13:50
  • That's sorted it, I was wondering if setting this on the checkboxes was right when I then put them into a vbox. Perfect, thanks bud. I'll green tick it now! – sark9012 Jul 21 '14 at 13:54