Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

createComponent() / createObject() is opening new (duplicated) application



  • Dear all,

    I have this:

    main.qml:

    import QtQuick 2.12
    import QtQuick.Controls 2.0
    import 'JavaScript.js' as JS
    
    ApplicationWindow {
    
        id: mainWindow
        visible: true
        width: 870
        height: 535
    
        property int idToDelete
        ....
    }
    

    InitialScreen.qml:

    import QtQuick 2.12
    import QtQuick.Controls 2.0
    import 'JavaScript.js' as JS
    
    Item {
        property int idToDelete: 0
        onIdToDeleteChanged: { JS.updateIdToDelete(idToDelete); }
        ....
    }
    

    JavaScript.js:

    var component;
    var sprite;
    function updateIdToDelete(id) {
        component = Qt.createComponent("main.qml");
        sprite = component.createObject(mainWindow, {idToDelete: id});
    
        if (sprite == null) {
            // Error Handling
            console.log("Error creating object");
        }
    }
    

    so my goal is, that as soon as idToDelete changes in InitialScreen.qml, via the JS script updateIdToDelete() it will update the value into main.qml as well (with same name).

    instead of doing so, it opens new window .exe application (even in debug mode), so i have the application window opened twice...

    what am I doing wrong here?



  • @shokarta said in createComponent() / createObject() is opening new (duplicated) application:

    function updateIdToDelete(id) {
    component = Qt.createComponent("main.qml");
    sprite = component.createObject(mainWindow, {idToDelete: id});

    you are creating your main.qml ( ApplicationWindow ) again with this code, so it is normal that it opens a new window

    you are trying to pass a value from InitialScreen Component to your main.qml right?
    How/where is your InitialScreen created ? If it is in you main.qml then you can simple do like this

    //main.qml

    ApplicationWindow {
       property int idToDelete : initialScreen.idToDelete
      
      InitialScreen{
       id: initialScreen
      }
    }
    


  • @shokarta said in createComponent() / createObject() is opening new (duplicated) application:

    function updateIdToDelete(id) {
    component = Qt.createComponent("main.qml");
    sprite = component.createObject(mainWindow, {idToDelete: id});

    you are creating your main.qml ( ApplicationWindow ) again with this code, so it is normal that it opens a new window

    you are trying to pass a value from InitialScreen Component to your main.qml right?
    How/where is your InitialScreen created ? If it is in you main.qml then you can simple do like this

    //main.qml

    ApplicationWindow {
       property int idToDelete : initialScreen.idToDelete
      
      InitialScreen{
       id: initialScreen
      }
    }
    


  • Dude, you are the live saver...

    I was sortof thinking of this solution too, but I was using it like this:

    ApplicationWindow {
    
        id: mainWindow
        StackView {
            id: stackView
            anchors.fill: parent
            initialItem: initialScreen
        }
    
        Component {
            id: initialScreen
            InitialScreen {}
        }
    }
    

    and this way initialScreen.idToDelete was not reachable.
    However redesigning code to what you suggested solved the problem :)

    Thank you a lot!


Log in to reply