Important: Please read the Qt Code of Conduct -

Properly destroying qml objects

  • Hello again,

    I'm a bit confused again with QML. I have a situation where I dynamically add objects to a row. When a new file is opened the row should be cleared from the dynamic objects.

    As of now I'm doing it like this, where layerRow is obviously and ID for Row item:

    @function clear() {
    for(var i=0; i<layerRow.children.length; i++)

    From C++ I invoke the function the following way:
    QMetaObject::invokeMethod(layerPanel, "clear");

    It kinda works, it clears the scene, but it doesn't happen immediately. Basically, for every object added to the Row I set objectName property to be L0, L1, L2 etc. So I'm able to find them and populate them from C++. The problem is when clear function is invoked, the C++ continues to execute the rest of the code before they are removed and doesn't find the new object properly, it finds the old and the new object.

    It does work, when the Row initially is clear, then in C++ I find only one object by name:
    (Layer_QMLTYPE_19(0x23791ce8, name = "L0") )

    And here is when I tried to clear and readd the items to the Row:
    (Layer_QMLTYPE_19(0x23791ce8, name = "L0") , Layer_QMLTYPE_19(0x2ccb83e0, name = "L0") )

    As you can see by the object address, it found the previous object, even though clear was invoked before.

    If I wait a bit, it will eventually return the children correctly.

    I kinda could avoid the problem by saving the addresses of previous objects and then finding the new object by checking that it didn't exist before. Or maybe I could connect destroyed signal and count if all of them were destroyed before continuing. I'm not particulary pleased with my ideas, hence, the question, is there a proper/better way of destroying items?


Log in to reply