Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. createComponent() / createObject() is opening new (duplicated) application
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 553 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by shokarta
    #1

    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?

    ODБOïO 1 Reply Last reply
    0
    • S shokarta

      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?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      @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
        }
      }
      
      1 Reply Last reply
      3
      • S Offline
        S Offline
        shokarta
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved