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 order object creation
Forum Updated to NodeBB v4.3 + New Features

How to order object creation

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 462 Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    In another topic I explored how to create a QML object from a C++ class. Now I need to control the order in which these objects are created. Here's what I'm trying to do:

    class Outcome : public QObject
    {
        Q_OBJECT
        QML_ELEMENT
        ...
    

    and in a QML file:

    // main.qml
    property Outcome outcome
    Component {
        id: outcomeComponent
        Outcome {
            id: newOutcome
        }
    }
    
    Component.onCompleted: {
        outcome = outcomeComponent.createObject(parent)
    }
    
    ListView {
        model: equipmentCopy
        delegate: DelegateChooser {
            role: "category"
            DelegateChoice {
                roleValue: NgaUI.CATEGORY_VSP
                delegate: VspEdit {}
            }
            ...
    

    the delegate is:

    // VspEdit.qml
    ColumnLayout {
        property Outcome outcomeCopy
        Component.onCompleted: {
            outcomeCopy = outcome
        }
        ...
    

    The outcomeCopy object in VspEdit.qml is created before the outcome object in main.qml, and is therefore null.

    Is there a way to control the order in which objects are created, or is there a better way overall to create this object?

    Thanks...

    dheerendraD 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      In another topic I explored how to create a QML object from a C++ class. Now I need to control the order in which these objects are created. Here's what I'm trying to do:

      class Outcome : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
          ...
      

      and in a QML file:

      // main.qml
      property Outcome outcome
      Component {
          id: outcomeComponent
          Outcome {
              id: newOutcome
          }
      }
      
      Component.onCompleted: {
          outcome = outcomeComponent.createObject(parent)
      }
      
      ListView {
          model: equipmentCopy
          delegate: DelegateChooser {
              role: "category"
              DelegateChoice {
                  roleValue: NgaUI.CATEGORY_VSP
                  delegate: VspEdit {}
              }
              ...
      

      the delegate is:

      // VspEdit.qml
      ColumnLayout {
          property Outcome outcomeCopy
          Component.onCompleted: {
              outcomeCopy = outcome
          }
          ...
      

      The outcomeCopy object in VspEdit.qml is created before the outcome object in main.qml, and is therefore null.

      Is there a way to control the order in which objects are created, or is there a better way overall to create this object?

      Thanks...

      dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @mzimmers

      True. After all the inside objects ready, it is coming to component.onCompleted of main. try some thing like this in main.qml

      property var outcome : outcomeComponent.createObject()

      Now use the outcome object for your assignment inside the delegate.

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

      mzimmersM 1 Reply Last reply
      1
      • dheerendraD dheerendra

        @mzimmers

        True. After all the inside objects ready, it is coming to component.onCompleted of main. try some thing like this in main.qml

        property var outcome : outcomeComponent.createObject()

        Now use the outcome object for your assignment inside the delegate.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @dheerendra excellent.

        property var outcome: outcomeComponent.createObject(parent)
        
        Component {
            id: outcomeComponent
            Outcome {
                id: newOutcome
            }
        }
        

        This approach also eliminated the need for creating the copy in the delegate; I can just use outcome directly (which is preferable).

        Thank you for the help on this.

        JKSHJ 1 Reply Last reply
        0
        • mzimmersM mzimmers has marked this topic as solved on
        • mzimmersM mzimmers

          @dheerendra excellent.

          property var outcome: outcomeComponent.createObject(parent)
          
          Component {
              id: outcomeComponent
              Outcome {
                  id: newOutcome
              }
          }
          

          This approach also eliminated the need for creating the copy in the delegate; I can just use outcome directly (which is preferable).

          Thank you for the help on this.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @mzimmers said in How to order object creation:

          I can just use outcome directly (which is preferable).

          Please don't do that! That is called "unqualified access", which prevents the QML compiler from optimizing your code.

          • See https://www.qt.io/blog/compiling-qml-to-c-fixing-unqualfied-access for a discussion about unqualified access
          • See https://www.qt.io/blog/the-numbers-performance-benefits-of-the-new-qt-quick-compiler for a peek at the optimization

          Use property bindings correctly (without doing special construction and assignment in Component.onCompleted) and you should get things the right order:

          // main.qml
          Window {
              id: window
              
              property Outcome outcome: outcomeComponent.createObject(window) as Outcome
              Component {
                  id: outcomeComponent
                  Outcome {}
              }
              
              ListView {
                  delegate: VspEdit {
                      outcome: window.outcome
                  }
              }
          }
          
          // VspEdit.qml
          ColumnLayout {
              id: vspEdit
              required property Outcome outcome
              Component.onCompleted: console.log(vspEdit.outcome)
          }
          

          Notice that I fully qualify my variables (window.outcome, vspEdit.outcome)

          property var outcome

          Use the actual type instead of var for more optimization (and also so that modern tools like qmllint can scan your code and help you find any issues)

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

          1 Reply Last reply
          1
          • JKSHJ JKSH referenced this topic on

          • Login

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