Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What am i missing in my loader code



  • Hi i am trying to use a Loader on my custom RowLayout defined in MyCustomRowLayout.qml
    when i use it normally it works fine:

    Item
    {
        id:item
       MyCustomRowLayout
       {
       id:row
       property alias context: item 
       }
    }
    

    But when i try to wrap it in Component and Loader it won't display:

    Item{ id item
    	Component
    	{
    		id:componentRow
    		MyCustomRowLayout
    		{
    			id:someRow
    			property alias context: item
    		}
    
    	}
    	Loader{
    		id: loaderr
    		sourceComponent:componentRow
    	}
    }
    

    What code should i add?



  • Hey,

    If this is a direct copy of your code there is a few typos..
    id name needs to be id:name.

    Also you cannot link your customrowlayout alias to the item reference which is outside your custom component..

    "Unlike an ordinary property, an alias has the following restrictions:

    It can only refer to an object, or the property of an object, that is within the scope of the type within which the alias is declared.
    It cannot contain arbitrary JavaScript expressions
    It cannot refer to objects declared outside of the scope of its type.
    The alias reference is not optional, unlike the optional default value for an ordinary property; the alias reference must be provided when the alias is first declared.
    It cannot refer to attached properties.
    It cannot refer to properties inside a hierarchy with depth 3 or greater. "

    Look here:
    https://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#property-aliases

    So you need to do it like this, or simlar :)

        Item{
            id:item
            Component
            {
                id:componentRow
                Row
                {
                    id:someRow
                    Rectangle {
                        width:500
                        height:20
                        color:"#ff1111"
                        property Item context:item
                        Text {
                            anchors.horizontalCenter: parent.horizontalCenter
                            text: "Some text.."
                        }
                    }
                }
    
            }
            Loader{
                id: loaderr
                sourceComponent:componentRow
            }
        }