0

I have a little problem in my program. I have a config file put in settings. I pull from it the names of the object I need to be checked (these are QCheckBox). I have this piece of code (It compiles and runs but when it's at "cBox->setChecked" it just crash):

void Preproc::on_tBtnManual_toggled(bool checked){

if(checked){
    ui->tBtnManual->setText("Systematic");
}else{
    ui->tBtnManual->setText("Manual");
    settings.beginGroup("Preprocessing");
    QStringList keys = settings.childKeys();
    foreach(QString configParam,keys){
        QCheckBox *cBox = ui->gridLayout->findChild<QCheckBox *>(configParam);
        cBox->setChecked(settings.value(configParam).toBool());
    }
 }

}

I have tried to put ui->cBox->... put it says that cBox is not a child of ui. If I qDebug(cBox) I have a QObject(0x0) so nothing !

I'm a little new to Qt so maybe it's a simple thing. Thanks and have a nice day :)

Pico
  • 303
  • 3
  • 9

2 Answers2

1

Are you sure that an object is found? I don't think so (different name? wrong layout?). cBox is 0x0 when nothing is found. However put a

if (cBox)

before

cBox->setChecked(settings.value(configParam).toBool());

and it will not crash anymore when it doesn't find an object by name.

asclepix
  • 7,971
  • 3
  • 31
  • 41
  • Yes it didn't crash anymore but I still have a QObject null. I checked the name and they're good. – Pico Jun 07 '13 at 16:28
0

are you sure the name (content of configParam) is correct? you can try the search from QApplication

QApplication::instance()->findChild<QCheckBox *>(configParam);

the findChild method performs a recursive search, if the object exists in the hirachie, it will be found. if the object is not found, it could be:

  • the object does not exist
  • the object has another name
  • the object or one of its ancestors has no (NULL) parent

can you post the part of the .ui file with the check box? it would be helpful.

WoJo
  • 360
  • 2
  • 10
  • Here is my settings.ini from where it gets its parameters `[Preprocessing] cBoxGain=true cBoxMask=true` – Pico Jun 07 '13 at 16:29
  • ` false Gain Error true ` I've shortened my code by deleting others QcheckBox. Also I always got the same null object with your code – Pico Jun 07 '13 at 16:31
  • you searched for "cBox" (ui->cBox->...), but the name is `name="cBoxGain"` try ui->cBoxGain (or `findChild("cBoxGain")` ) – WoJo Jun 09 '13 at 16:03