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. [solved] Dynamically adding items
Forum Updated to NodeBB v4.3 + New Features

[solved] Dynamically adding items

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 870 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.
  • D Offline
    D Offline
    digorydoo
    wrote on last edited by
    #1

    I want to dynamically add items to a custom component when it's created. According to
    http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.html , I can do this by creating an external JavaScript file,

    @
    import "MyNavDrawer.js" as Scripts;
    @

    and call it from onCompleted:

    @
    Component.onCompleted: { Scripts.createItems(); }
    @

    My JavaScript file looks like this:

    @
    var listViewComponent;

    function createItems()
    {
    console.log ("createItems");

    listViewComponent = Qt.createComponent ("MyNavDrawerListView.qml");
    
    if (listViewComponent.status === Component.Ready)
    {
        console.log ("ready");
        createItemsFinish();
    }
    else
    {
        console.log ("delayed");
        listViewComponent.statusChanged.connect (createItemsFinish);
    }
    
    console.log ("end of createItems");
    

    }

    function createItemsFinish()
    {
    console.log ("createItemsFinish");

    if (listViewComponent.status !== Component.Ready)
    {
        console.log ("createItemsFinish: Component still not ready! " + listViewComponent.errorString());
        return;
    }
    
    for (var i = 0; i < items.length; i++)
    {
        console.log (items[i].caption);
        var v = listViewComponent.createObject (drawerFrame, { "data": items[i] });
    
        if (v === null)
        {
            console.log ("createItemsFinish: Failed to create item " + i + "!");
            return;
        }
    

    }
    }
    @

    createItems is in fact called, but the signal handler createItemsFinish isn't. The log output is:

    @
    qml: createItems
    qml: delayed
    qml: end of createItems
    @

    What am I doing wrong?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      Are you sure MyNavDrawerListView.qml can be found? Add this line to the end of createItems():

      @
      console.log(listViewComponent.status)
      @

      Compare the result with http://qt-project.org/doc/qt-5/qml-qtqml-component.html#status-prop

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • D Offline
        D Offline
        digorydoo
        wrote on last edited by
        #3

        Oops, you're right! The status is 3 (Component.Error), and the errorString showed there was an error in the qml file I was trying to load. Thanks for the hint!

        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