1
votes

In my app I have a custom item derived from QObject. In specified moment I need to access its parent. But since this item is not QQuickItem descendant there is no parentItem() here.

QML:

Item {
    id: root
    MyItem {
        id: myitem
    }
}

C++

class MyItem : public QObject {
public:
    explicit MyItem(QObject *parent = 0) {
        // parent is always NULL here
    }
}

So my question - how can I access parent of non-visual QML item in C++?

1
QObject has the parent() method.Dmitry Sokolov

1 Answers

2
votes

As far as I know, there is no way to access the parent item from the MyItem constructor. QML first constructs the object, then sets its parent property to the appropriate item from QML hierarchy.

However, after the object is constructed, calling the QObject::parent method will give the results you expect. By the time when Component.onCompleted is called, the parent will be set. You can finish your object initialization with parent data from that handler.