【问题标题】:How to assign object's parents to QObjectList?如何将对象的父级分配给 QObjectList?
【发布时间】:2021-03-11 21:43:49
【问题描述】:

我可以将此代码用于分配孩子。

QObjectList boxes = this->children();

但我无法使用此代码。

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

我如何获得父母?

【问题讨论】:

    标签: c++ qt oop inheritance


    【解决方案1】:

    没有获取父母的默认方法,因此您必须使用parent() 方法进行迭代:

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

    【讨论】:

      猜你喜欢
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多