Qt 5: read property inside bootloader

How to read the timeout property located inside a Loader object in Qt5 QML Quick 2.0?

 import QtQuick 2.0 Rectangle { width: 100 height: 100 color: "black" property Component comp1 : Component { Rectangle { id: abc property int timeout: 5000 width: 10; height: 10; color: "red" } } Loader { id: loader sourceComponent: comp1 } Component.onCompleted: console.log( "timeout: " + loader.item.abc.timeout ) } 

TypeError: Cannot read timeout property from undefined

+6
source share
1 answer

You have several problems in your code, namely:

1) You do not assign an id your component object.

2) You are trying to inherit Component with a property that is useless in this simple code.

3) You are not using the item property correctly for the Loader element.

4) You refer to the name of the property, not to the id component. This again returns to unnecessary inheritance.

Based on the official documentation, you should do something like this:

 import QtQuick 2.0 Rectangle { width: 100 height: 100 color: "black" Component { id: comp1 Rectangle { id: abc property int timeout: 5000 width: 10; height: 10; color: "red" } } Loader { id: loader sourceComponent: comp1 } Component.onCompleted: console.log( "timeout: " + loader.item.timeout ) } 
+4
source

All Articles