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. Issue in accessing a property with a loader in qml
Forum Updated to NodeBB v4.3 + New Features

Issue in accessing a property with a loader in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 768 Views
  • 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.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by
    #1

    Hi Team,

    I am a basic learner in QML. I am facing an issue in accessing a property with a loader.
    I am getting an error like below
    "TypeError: Cannot read property 'rectId' of null"

    But from the Qt doc, I came to know, we can access an object property defined in another qml via loader's item property.

    But in my sample program, I am not able to access an rectId object's property.
    Can someone please correct me.

    Below is my sample code
    main.qml

    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        // Loader is defined in main.qml
        Loader {
            id: mainloader
            source: "qrc:/sample.qml"
        }
    }
    

    sample.qml

    Item {
        id: sampleId
        width: 100
        height: 100
    
        Rectangle {
            id: rectId
            property int val: 1
            anchors.fill: parent
            color: "red"
        }
    
        Component.onCompleted: {
            console.log("Trying to access rectId property through Loader's item = ", **mainloader.item.rectId.val**)
        }
    }
    
    1 Reply Last reply
    0
    • oria66O Offline
      oria66O Offline
      oria66
      wrote on last edited by oria66
      #2

      Mmm, why do you want to access loader item properties inside the same item component? I mean Sample.qml, as a standalone component, should not know if it going to be called by a Loader, Stackview, etc.

      First, in your example in the console.log line you just use rectId.val, not mainloader.item.rectId.val.

      Second if want to use the properties of your item used in the Loader, you must expose as alias the Rectangle or as a property the val variable

      sample.qml

      Item {
          id: sampleId
          width: 100
          height: 100
      
          property alias rectIdAlias: rectId // First mode
          property int valExposed: rectId.val // Second mode
      
          Rectangle {
              id: rectId
              property int val: 1
              anchors.fill: parent
              color: "red"
          }
      
          Component.onCompleted: {
              console.log("Trying to access rectId property = ", rectId.val)
          }
      }
      

      main.qml

      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          // Loader is defined in main.qml
          Loader {
              id: mainloader
              source: "qrc:/sample.qml"
              onStatusChanged: if (loader.status == Loader.Ready) {
              console.log("Trying to access rectId property through Loader's item from main with Alias = ", mainloader.item.rectIdAlias.val)
              console.log("Trying to access rectId property through Loader's item from main with property = ", mainloader.item.valExposed)
      }
          }
      }
      

      Hope this clarify your doubts.

      The truth is out there

      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