2012-03-27 49 views
2

我创建了一个QRadioButton数组,并试图用六个radioButton来初始化它。在我用对象填充数组的时候,我没有收到任何警告或错误。但是,当我尝试查看是否单选按钮是检查我碰到所有单选按钮,但不是阵列的第一项。 下面是代码:QRadioButton Array issue

// 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 
    } 

然后让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"; 
+0

为什么您使用的foreach执行罚款?这不会编译。改为使用 – pnezis 2012-03-27 10:38:27

回答

2

不知道到底是什么原因造成的崩溃,但我建议你利用C++的功能和Qt容器,而不是操纵Ç样式数组。试试这个:

// 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); 
} 

你的代码的其余部分应该