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

[Non-qmlsc] `State.when` evaluation fails when an object is bound

    XMLWordPrintable

Details

    • dfdba3b12 (dev), 498de3e38 (6.4), 55ca13df5 (6.5), 045e3de7a (tqtc/lts-6.2)

    Description

      We get different outcomes, depending on whether or not qmlsc is run:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      
      Window {
          id: win
      
          width: 640
          height: 480
          visible: true
          title: "Base State"
      
          property var obj: null
      
          Rectangle {
              id: redRect
              width: 100
              height: 100
              color: "red"
              visible: win.obj
          }
      
          Item {
              states: [
                  State {
                      id: otherState
                      name: "other"
                      when: win.obj
                      PropertyChanges {
                          target: win
                          title: "Other State"
                      }
                  }
              ]
          }
      
          // Report variable values, before and after creating object
          function report() {
              console.log("win.obj:", win.obj)
              console.log("redRect.visible:", redRect.visible)
              console.log("otherState.when:", otherState.when)
              console.log("win.title:", win.title, '\n')
          }
      
          // Initial report
          Component.onCompleted: win.report()
      
          Timer {
              interval: 1000
              running: true
              repeat: false
              onTriggered: {
                  win.obj = Qt.createQmlObject("import QtQml 2.15\nQtObject {}",
                      win, "myDynamicSnippet"
                  )
                  // Final report
                  win.report()
              }
          }
      }
      

       

      Output when qmlsc is used (Correct)

      qml: win.obj: null
      qml: redRect.visible: false
      qml: otherState.when: false
      qml: win.title: Base State 
      
      qml: win.obj: QObject(0x1911d2c59b0)
      qml: redRect.visible: true
      qml: otherState.when: true
      qml: win.title: Other State 
      

       

      Output when qmlcachegen is used, or when bytecode compilation is disabled altogether (Wrong)

      qml: win.obj: null
      qml: redRect.visible: false
      qml: otherState.when: false
      qml: win.title: Base State 
      
      qml: win.obj: QObject(0x1a958155510)
      qml: redRect.visible: true
      qml: otherState.when: true
      qml: win.title: Base State 
      

       

      Workaround

      Explicitly coerce the object into a Boolean value:

      • win.obj !== null
      • Boolean(win.obj)

      Attachments

        Issue Links

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

          Activity

            People

              fabiankosmale Fabian Kosmale
              skoh-qt Sze Howe Koh
              Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes