Important: Please read the Qt Code of Conduct -

QML context of dynamically generated items

  • Hello,

    I'm trying to figure out in which context dynamically generated items are being stored?
    Therefor let me explain my scenario.

    First I've registered two new QML types:
    @qmlRegisterType<MyViewController>("uicontrollers", 1, 0, "ViewController");
    qmlRegisterType<MyTabController>("uicontrollers", 1, 0, "TabController");@

    Then I used them in qml files:

    @Rectangle {

        id: tab_factory
        MyTab {}
        id: a_tabview
        //some code here ...
        id: view_controller
        objectName: "myViewController"
            var tab_obj = tab_factory.createObject(a_tabview)
            var a_tab = a_tabview.addTab(a_name, tab_obj)
   = true


    id: the_tab

    Rectangle {
        id: tab_frame
        border.width: 0
        property string the_name: ""
            id: a_tab_split
            // some items here ...
            id: my_tab_controller
            objectName: "myTabController"


    Dynamic tab creation works fine from my C++ class.
    Therefor I use the function described below to find my ViewController and emit newTab signal.
    But when I try to find the created TabController in the QML context I get no results:

    //** Template FindMyQMLObjects
    template<typename T> QList<T> FindQMLObjects(QString in_name)
    QList<T> ret;
    QList<QObject*> root_obj_list = m_engine.rootObjects();

    foreach(QObject* obj, root_obj_list)
        qDebug() << "root_obj name: " << obj->objectName();
        ret = obj->findChildren<T>(in_name, Qt::FindChildrenRecursively);
        if (ret.size() > 0) break;
    return ret;


    Any idea where my QML objects are hiding?

    Thanks for your help.
    Best regards,

  • Qt Champions 2017

    Since you are passing the parent as Tab_view, it should be children of the tabview only. It will be in the current context itself. Here is the sample which I tried and it works perfectly.

    I suspect that either object is not created or you are not iterating the correct object. Try with following code snippet. It should work

    QQuickView view;

    QQuickItem *item = view->rootObject();
    QQuickItem *obj = qobject_cast<QQuickItem >(item->findChild<QQuickItem>("myObj"));
    if (obj)
    qDebug() << "My name is ="<<obj->objectName();

  • Dheerendra: Thanks for your reply.

    Since I'm using
    @QQmlApplicationEngine m_engine;@
    in my application and searching over all possible rootObjects @QList<QObject*> root_obj_list = m_engine.rootObjects();@
    @ret = obj->findChildren<T>(in_name, Qt::FindChildrenRecursively);@
    I'm quite sure that I'm iterating over all possible Items, e.g. searching the ViewController works fine.

    Due to log output I'm even quite sure that the TabController item is created correctly.

    I have tried some variations to access the TabController from ViewController in qml file:

    var a_tab = a_tabview.addTab(a_name, tab_obj)
    var a_item = a_tab.item // this is the Rectangle in MyTab.qml
    var a_tab_controller = a_tab.TabController // this returns some [object Object] but I don't know how I can access the TabController item within!?!
    var res_tab_controller = a_tab.item.resources[0] // this returns the TabController item but I think it's not a proper way to retrieve resources[0]

    This happens before I search the TabController from C++ code.

  • Qt Champions 2017

    How are you giving object name ? For createObject can you pass the objectName as well ?. You can try some thing like the following and see

    @ var obj = component.createObject(parent, {"objectName" :"myObj", "x": 100, "y": 100});@

  • I have defined the objectName in MyTab.qml.
    findChildren<>() should return a list of all Items with the given objectName.

    Any suggestions how I can access the TabController within
    @var a_tab_controller = a_tab.TabController@ which returns some
    @[object Object]@ ?

Log in to reply