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

Javascript "as casting" doesn't work with inline components

XMLWordPrintable

    • All

      When running the attached example and clicking in the two rectangles you can see that the "as casting" works with Components defined in separate files but not with components defined inline

       

      Separate files

      qml: myOtherRect MyOtherRectangle_QMLTYPE_0(0x6000006edce0)
      qml: myOtherRect.isAwesome true
      qml: x MyOtherRectangle_QMLTYPE_0(0x6000006edce0)
      qml: x.isAwesome true

       

      inline component

      qml: myRect MyRectangle(0x6000006ec9a0)
      qml: myRect.isAwesome true
      qml: x null
      qrc:/qt/qml/untitled/Main.qml:28: TypeError: Cannot read property 'isAwesome' of null
      

       

      I'll put the files contents here and also attach the complete project in case that's easier for reproducing

      Main.qml

       

      import QtQuick
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
          component MyRectangle : Rectangle {
              property bool isAwesome: true
          }
          MyRectangle {
              id: myRect
              color: "orange"
              height: parent.height
              width: parent.width / 2
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      console.log("myRect", myRect);
                      console.log("myRect.isAwesome", myRect.isAwesome);
                      var x = myRect as MyRectangle;
                      console.log("x", x);
                      console.log("x.isAwesome", x.isAwesome);
                  }
              }
          }
          MyOtherRectangle {
              id: myOtherRect
              color: "yellow"
              height: parent.height
              width: parent.width / 2
              anchors.right: parent.right
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      console.log("myOtherRect", myOtherRect);
                      console.log("myOtherRect.isAwesome", myOtherRect.isAwesome);
                      var x = myOtherRect as MyOtherRectangle;
                      console.log("x", x);
                      console.log("x.isAwesome", x.isAwesome);
                  }
              }
          }
      }
      

      MyOtherRectangle.qml

       

      import QtQuick
      Rectangle {
          property bool isAwesome: true
      }
      

       

       

       

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

            qtqmlteam Qt Qml Team User
            albert.astals.cid.avid Albert Astals Cid
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes