2

I create a QRadioButton array and tried to init it with six radioButtons. At the moment when i populate array with object i do not receive any warnings or errors. But when i try to see if radio button is check i get crash for all radio button , but not for first item from array. Here is the code :

// rd is declared in .h as  QRadioButton *rd[6];

   for (int c=0,c<6,c++) {
           rd[c] = new QRadioButton("name");
           verticalBox->addWidget(rd[c]);  // it's a layout
   }

Then make cheking :

  if (rd[0]->isChecked() == true)
        qDebug()<<"checked";
    else if (rd[1]->isChecked() == true)
        qDebug()<<"checked";
    else if (rd[2]->isChecked() == true)
        qDebug()<<"checked";
   else if (rd[3]->isChecked() == true)
        qDebug()<<"checked";
    else if (rd[4]->isChecked() == true)
        qDebug()<<"checked";
    else if (rd[5]->isChecked() == true)
        qDebug()<<"checked";
develoops
  • 545
  • 2
  • 10
  • 21

1 Answers1

2

Don't know exactly what is causing your crash, but I suggest you to leverage C++ features and Qt containers instead of manipulating C style arrays. Try this:

// rd is declared in .h as  QList<QRadioButton*> rd;
for (int i=0; i<6; ++i) {
  QRadioButton * radio_btn = new QRadioButton("name");
  rd << radio_btn; // append radio button to the list
  verticalBox->addWidget(radio_btn);
}

The rest of your code should execute fine

Masci
  • 5,864
  • 1
  • 26
  • 21