Access properties from dynamically loaded component



  • 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.



  • 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 : ""
        }
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.