Selçuk Altınay
Selçuk Altınay

Reputation: 69

How to assign object's parents to QObjectList?

I can use this code for the assign children.

QObjectList boxes = this->children();

But i can't use this code.

QObjectList boxes = ui->checkBox_2->parent();

How can i obtain parents?

Upvotes: 1

Views: 62

Answers (1)

eyllanesc
eyllanesc

Reputation: 243983

There is no default method to get parents so you have to iterate using the parent() method:

QObjectList parents(QObject *qobject){
    if(!qobject)
        return {};
    QObjectList parents;
    const QObject *o = qobject;
    while (QObject *q = o->parent()) {
        parents.push_back(q);
        o = q;
    }
    return parents;
}
QObjectList boxes = parents(ui->checkBox_2);

Upvotes: 2

Related Questions