Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-51995

Loader doesn't immediately destroy the loaded item, and hence cannot be used to ensure a loaded item can freely reference properties that are only valid while the item is loaded

    XMLWordPrintable

Details

    • 2eb2d6386da304cd1164264ae0bff685c796d89c

    Description

      A game often has various states, like loading, running, paused, invalid, etc. The UI of the game should only access the game's properties while the game is in a certain state. For convenience, and to give the UI a heads up before the state enters one that is "dangerous", I define an isReady property:

      main.qml
      import QtQuick 2.4
      import QtQuick.Window 2.2
      import QtQuick.Controls 1.0
      
      Window {
          id: window
          visible: true
      
          Item {
              id: theGame
      
              property bool isReady: false
      
              onStateChanged: {
                  if (state == "invalid") {
                      print("game.isReady about to change to false...")
                      isReady = false;
                      print("... game.isReady changed to " + isReady + "; nothing should reference properties of game now")
      
                      player.destroy();
                      player = null;
                  } else if (state == "running") {
                      player = Qt.createQmlObject("import QtQuick 2.0; Item { property color color: 'black' }", window);
      
                      print("game.isReady about to change to true...")
                      isReady = true;
                      print("... game.isReady changed to true")
                  }
              }
      
              property Item player
          }
      
          Column {
              Button {
                  text: "Load Game"
                  onClicked: {
                      theGame.state = "running";
                  }
              }
      
              Button {
                  text: "Quit Game"
                  onClicked: {
                      theGame.state = "invalid";
                  }
              }
              Loader {
                  active: theGame.isReady
                  Component.onCompleted: setSource("qrc:/LoaderItem.qml", { "game": theGame })
              }
          }
      }
      
      LoadedItem.qml
      import QtQuick 2.0
      
      Rectangle {
          width: 400
          height: 400
          color: game.player.color
      
          property var game
      }
      

      The Loader that loads my UI is only active when the game is ready. This is extremely useful to avoid clutter like this in the UI code:

      color: game.isReady ? game.player.color : "transparent"
      

      It means that the code in LoadedItem can always safely assume that the game and its properties are available and safe to access.

      However, if you run the application, you'll see that the item is still alive and trying to refer to properties of the game after it's entered an invalid state:

      qml: game.isReady about to change to true...
      qml: ... game.isReady changed to true
      qml: game.isReady about to change to false...
      qml: ... game.isReady changed to false; nothing should reference properties of game now
      qrc:/LoaderItem.qml:6: TypeError: Cannot read property 'color' of null
      

      I believe that the problem is this code. I'm not sure what situtation that comment is referring to. Why would the loaded item be responsible for loading another item? That should be done by code in the parent scope/context.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              aalpert Alan Alpert
              mitch_curtis Mitch Curtis
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes