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. How to create QQmlComponent child instances inside extended QQuickItem (C++)

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

Scheduled Pinned Locked Moved QML and Qt Quick
c++qquickitemqqmlcomponent
4 Posts 3 Posters 2.9k Views
  • 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 Offline
    C Offline
    chocis
    wrote on last edited by
    #1

    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
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Did you try this and setting QQmlComponent::Asynchronous ?

      157

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chocis
        wrote on last edited by
        #3

        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
        1
        • jiancaiyangJ Offline
          jiancaiyangJ Offline
          jiancaiyang
          wrote on last edited by
          #4

          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
          0

          • Login

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