How to order object creation
-
wrote on 25 Apr 2024, 01:35 last edited by
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...
-
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...
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.
-
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.
wrote on 25 Apr 2024, 13:54 last edited by@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.
-
-
@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.
@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) -
1/4