Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML context of dynamically generated items
Forum Updated to NodeBB v4.3 + New Features

QML context of dynamically generated items

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ttuna
    wrote on last edited by
    #1

    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:

    mainview.qml
    @Rectangle {

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

    }@

    MyTab.qml
    @Component
    {
    id: the_tab

    Rectangle {
        id: tab_frame
        border.width: 0
    
        property string the_name: ""
    
        SplitView
        {
            id: a_tab_split
    
            // some items here ...
        }
    
        TabController
        {
            id: my_tab_controller
            objectName: "myTabController"
    
            Component.onCompleted:
            {
                console.log(my_tab_controller)
            }
        }
    }
    

    }
    @

    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:

    uicontroller.h
    @//*****************************************************************************
    //** 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,
    Rainer

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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;
      view.setSource(QUrl(QStringLiteral("qrc:///main.qml")));
      view.show();

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ttuna
        wrote on last edited by
        #3

        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();@
        with
        @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.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

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

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ttuna
            wrote on last edited by
            #5

            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]@ ?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved