My advise (as most of the time on the forum :P ), would be to not do things like you are doing.
Manipulating UI items is not very mainenable and flexible. You should act on your data instead.
I don't really know what is your use case, but most of the time you should avoid using Qt.createObject or Qt.createComponent and let view/models class handle the instantiation of your dynamic objects.
In your example, you could use a Repeater with a model (an int, a js array, a ListModel, ...). I rewrote it with a ListModel :
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel {
id: listModel
}
ColumnLayout {
RowLayout {
Button {
text: qsTr("Add")
onClicked: listModel.append({text: listModel.count + 1})
}
Button {
text: qsTr("Remove")
onClicked: {
if (listModel.count > 0)
listModel.remove(0);
}
}
Button {
text: qsTr("Clear & Add New 3")
onClicked: {
listModel.clear();
for (var i = 0; i < 3; ++i) {
listModel.append({text: listModel.count + 1})
}
}
}
}
ColumnLayout {
Repeater {
model: listModel
Label { // you can replace that with OwnLabel
text: "My text: %1, my index: %2".arg(model.text).arg(model.index)
}
}
}
}
}