Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to create QQmlComponent child instances inside extended QQuickItem (C++)

    QML and Qt Quick
    c++ qquickitem qqmlcomponent
    3
    4
    2478
    Loading More Posts
    • 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.
    • C
      chocis last edited by

      Hi, I need to implement custom QML componont which will create dynamic child elements depending on passed in configuration.
      Simplified example - I need to completely reproduce this QML code using only C++.

      Item {
          Button { text: "hello" }
          CheckBox { text: "click" }
      }
      

      For now I have created custom DynamicView object which extends QQuickItem. What I want to do is to create many child objects inside this object on instantiation.

      1. At what point in this object I can attach and create these child objects dynamically and what I need to be careful of, when destroying this object?

      I was successful to do it in componentComplete() virtual function using QQmlEngine,QQmlComponent,QQuickItem,setParent() like in documentation, BUT seems like it doesn't work the same way as pure QML, because I am using Loader in asynchronous mode and I can see that object creation blocks the main GUI.

      Big picture (what I want to achieve):

      Component {
          id: dynView
          DynamicView {
              customParameter: "create 40 checkboxes, 20 buttons, 30 inputfields" // mockup example
          }
      }
      
      Loader {
          id: loader
          asynchronous: true
      }
      

      When something is triggered, then Loader will destroy previous DynamicView and create a new one with different dynamic parameters.
      And because of many DynamicView children instantiations, it must be done asynchronously.

      1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        Hi,

        Did you try this and setting QQmlComponent::Asynchronous ?

        157

        1 Reply Last reply Reply Quote 0
        • C
          chocis last edited by

          Thanks, yes I tried that and that won't work, because:

          1. The .qml loading/compiling is done only once and that is not the bottleneck, so there is no big benefit of using Asynchronous.

            QQmlComponent *component = new QQmlComponent(engine, QUrl(QStringLiteral("qrc:/MyItem.qml")), QQmlComponent::Asynchronous);
            

          The real "freeze" is at Component instantiation (just simulating many object creation, because I've got powerful PC):

                 for (int i = 0; i < 1000; i++) {
                     QQuickItem *childItem = qobject_cast<QQuickItem*>(component->create());
                     childItem->setParentItem(this);
                 }
          

          I need the "create()" part to be Async.

          1. If I do the "Async" stuff manually myself, then there is no flexibility of Loader (asynchronous: true) property, in case I want to create the object somewhere in sync mode. ( Because I like the "correct" solutions) :)
          1 Reply Last reply Reply Quote 1
          • jiancaiyang
            jiancaiyang last edited by

            I have no idea on your issue, but I recall a method which is in Component type:
            object incubateObject(Item parent, object properties, enumeration mode)

            By noting "incubate" it means the object is created asynchronously, without blocking the GUI.

            我们自己的论坛:http://qtdream.com
            擅长三维角色仿真动画。

            1 Reply Last reply Reply Quote 0
            • First post
              Last post