Objects created by Qt.createQMLObject behave strangely when creator destroyed



  • Hi,

    I am trying to create instances of objects using Qt.createQmlObject and then from the same function destroy the object doing the creation. When I do this the objects seem to remain but all their javascript functions cease to work. If the parent is not destroyed everything works as normal. I am specifying a parent in the call to createQmlObject that is not destroyed so I am confused as to why the objects cease to function normally.

    I've included an example down the bottom, main.qml is the qml to run.

    In this example you would expect to be able to able to click the red box, have it make a new red box and that new red box is then able to be clicked again and a new one made and old one destroyed and so on. In fact only the first red box reacts to clicks and the dynamically created one doesn't.

    So my question is why does this happen and how could I get around it?

    I have a need to be able to create new objects and destroy the old one dynamically. I also can't use createComponent/createObject as there are other issues with that approach.

    Thanks in advance,

    Kimble Young

    main.qml

    @import QtQuick 1.0

    Rectangle {
    id: mainRectangle

    width: 360
    
    height: 360
    
    
    
    Component.onCompleted: {
        Qt.createQmlObject("import QtQuick 1.0; BoxThing { x:" + Math.random() * 360 + "; y:" + Math.random() * 360 + "}", mainRectangle );
    }
    

    }@

    BoxThing.qml

    @import QtQuick 1.0

    Rectangle {

    id: boxThing
    
    width: 50
    
    height: 50
    
    color: "red"
    
    MouseArea {
    
        anchors.fill: parent
    
        onClicked:  {
    
            console.log("I have been clicked");
    
            Qt.createQmlObject("import QtQuick 1.0; BoxThing { x:" + Math.random() * 360 + "; y:" + Math.random() * 360 + "}", mainRectangle);
    
            boxThing.destroy();
    
        }
    
    }
    

    }@



  • main.qml
    @import QtQuick 1.0

    Rectangle {
    id: mainRectangle

    property variant boxThing: null
    
    function createObj() {
        if (boxThing)
            boxThing.destroy();
    
        boxThing = Qt.createQmlObject("import QtQuick 1.0; BoxThing { x:" + Math.random() * 360 + "; y:" + Math.random() * 360 + "}", mainRectangle );
    }
    
    width: 360
    height: 360
    
    Component.onCompleted: {
        createObj()
    }
    
    Connections {
        target: boxThing
        onClicked: createObj()
    }
    

    }@

    BoxThing.qml
    @import QtQuick 1.0

    Rectangle {
    id: boxThing

    signal clicked()
    
    width: 50
    height: 50
    color: "red"
    
    MouseArea {
        anchors.fill: parent
        onClicked: boxThing.clicked()
    }
    

    }@



  • Diph,

    I appreciate the response. While that does get around the issue it doesn't address the problem. In my case I want to be able to have functions in BoxThing that can create and destroy instances of itself. Even multiple instances (which is especially why your example wouldn't work).

    So would I be right in saying it is caused by a scoping issue? Where the scope for the object created is the destroyed object? It seems like a bug to me but before I file one does anyone with more knowledge of createQmlObject() want to wade in?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.