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. Access properties from dynamically loaded component
Forum Updated to NodeBB v4.3 + New Features

Access properties from dynamically loaded component

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
loaderbindingproperties
2 Posts 2 Posters 5.9k 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.
  • M Offline
    M Offline
    morte
    wrote on last edited by morte
    #1

    Hello, how i can access loader scope properties (property1, property2) from dynamically loaded component (MyComponent.qml)?

    //main.qml:
    import QtQuick 2.5
    
    Item {
    	id: main
    	width: 200; height: 200
    	property string property1: "secret"
    	property int property2: 777
    
    	Loader {
    		id: loader
    		source: "MyComponent.qml"
    	}
    }
    
    //MyComponent.qml:
    import QtQuick 2.0
    
    Rectangle {
    	TextEdit {
    		id: text_edit
    		anchors.centerIn: parent
    		//text: main.property1+main.property2
    	}
    }
    

    I know that it can be done via Binding, stateless JS library var's, but that requires to make copies, i wondering is there way to to access properties directly.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christopher
      wrote on last edited by Christopher
      #2

      Maybe

      //main.qml:
      import QtQuick 2.5
      
      Loader {
          id: loader
          source: "MyComponent.qml"
          
          onLoaded: {
              loader.item.property1 = "secret"
              loader.item.property2 = 777
          }
      }
      
      //MyComponent.qml:
      import QtQuick 2.0
      
      Rectangle {
          id: rect
      
          property string property1
          property int property2
      
          TextEdit {
              id: text_edit
              anchors.centerIn: parent
              text: property1 + property2
          }
      }
      
      

      or

      //main.qml:
      import QtQuick 2.5
      
      Item {
          id: main
          width: 200; height: 200
          property string property1: "secret"
          property int property2: 777
      
          Loader {
              id: loader
              source: "MyComponent.qml"
      
              onLoaded: loader.item.text = property1 + property2
          }
      }
      
      //MyComponent.qml:
      import QtQuick 2.0
      
      Rectangle {
          id: rect
      
          property alias text: text_edit.text
          TextEdit {
              id: text_edit
              anchors.centerIn: parent
              text: property1 + property2
          }
      }
      
      

      or

      import QtQuick 2.0
      
      Rectangle {
          id: rect
          TextEdit {
              id: text_edit
              anchors.centerIn: parent
              // rect.parent -> loader; rect.parent.parent -> Item with id 'main'
              text: (rect.parent.parent.property1 !== undefined) ? rect.parent.parent.property1 : ""
          }
      }
      
      
      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