Pico
Pico

Reputation: 303

Use an QObject found by it's QObjectName

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 :)

Upvotes: 0

Views: 789

Answers (2)

WoJo
WoJo

Reputation: 370

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.

Upvotes: 0

asclepix
asclepix

Reputation: 8061

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.

Upvotes: 1

Related Questions