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.
  • M Offline
    M Offline
    mzimmers
    wrote on 25 Apr 2024, 01:35 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...

    D 1 Reply Last reply 25 Apr 2024, 06:30
    0
    • M mzimmers
      25 Apr 2024, 01:35

      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...

      D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 25 Apr 2024, 06:30 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

      M 1 Reply Last reply 25 Apr 2024, 13:54
      1
      • D dheerendra
        25 Apr 2024, 06:30

        @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.

        M Offline
        M Offline
        mzimmers
        wrote on 25 Apr 2024, 13:54 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.

        J 1 Reply Last reply 27 Apr 2024, 14:55
        0
        • M mzimmers has marked this topic as solved on 25 Apr 2024, 13:54
        • M mzimmers
          25 Apr 2024, 13:54

          @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.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 27 Apr 2024, 14:55 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
          • J JKSH referenced this topic on 27 Apr 2024, 14:57

          1/4

          25 Apr 2024, 01:35

          • Login

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