Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML property weak reference?

QML property weak reference?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 378 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.
  • E Offline
    E Offline
    Evgeniy Dushistov
    Qt Champion 2021
    wrote on last edited by
    #1

    I created object with Qt.createComponent , saved reference to property and then I called destroy for object in onClosed signal handler.
    And what I can not understand is why property becomes null after destroyed without manually setting to null.
    I thought that property is "strong reference", so even after destroying it will hold reference to some kind of "zombie" until I set it to null manually, but looks like property is some kind of "weak reference"?

    ApplicationWindow {
           id: applicationWindow
          property MyPopup dlg: null
          
         function show() {
            const comp = Qt.createComponent("qrc:/MyPopup.qml");
            const params = {parent: applicationWindow.contentItem};
            applicationWindow.dlg = comp.createObject(params.parent, params);
            console.assert(applicationWindow.dlg !== null, "MyPopup creation failed");
            applicationWindow.dlg.open();
        }
    
    Popup {
        onClosed: {
            console.log("onClosed was called", view);
            view.destroy();
        }
    
    jeremy_kJ 1 Reply Last reply
    0
    • E Evgeniy Dushistov

      I created object with Qt.createComponent , saved reference to property and then I called destroy for object in onClosed signal handler.
      And what I can not understand is why property becomes null after destroyed without manually setting to null.
      I thought that property is "strong reference", so even after destroying it will hold reference to some kind of "zombie" until I set it to null manually, but looks like property is some kind of "weak reference"?

      ApplicationWindow {
             id: applicationWindow
            property MyPopup dlg: null
            
           function show() {
              const comp = Qt.createComponent("qrc:/MyPopup.qml");
              const params = {parent: applicationWindow.contentItem};
              applicationWindow.dlg = comp.createObject(params.parent, params);
              console.assert(applicationWindow.dlg !== null, "MyPopup creation failed");
              applicationWindow.dlg.open();
          }
      
      Popup {
          onClosed: {
              console.log("onClosed was called", view);
              view.destroy();
          }
      
      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      @Evgeniy-Dushistov I had not noticed that previously, and there's no obvious mention of it in the documentation. Thanks! I see the same thing with this code:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      
      Window {
          id: root
      
          property Rectangle myRect
      
          Component {
              id: myComponent
              Rectangle { color: "yellow" }
          }
      
          MouseArea {
              anchors.fill: parent
              onClicked: {
                  if (loader.status == Loader.Null) {
                      loader.sourceComponent = myComponent;
                      myRect = loader.item;
                  }
                  else {
                      loader.sourceComponent = null;
                  }
              }
          }
      
          Loader {
              id: loader
              anchors.fill: parent
          }
      
          Text { anchors.centerIn: parent; text: "myRect: " + root.myRect }
      }
      

      My guess is that the properties that hold a QObject are tracked with a QPointer, but I'm not familiar enough with the implementation to find the answer quickly.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0

      • Login

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