[solved] Non-visual object in QML



  • Is it possible to make a non-visual QML component which holds properties and nested elements? I've tried the following code, but it's an error "Component elements may not contain properties other than id":
    @
    Component {
    property int aProperty: 5
    onAPropertyChanged: {
    someCode();
    }
    NestedObject {
    }
    }
    @

    I've also tried QtObject instead of Component, but there is another error with it: "Cannot assign to non-existent default property".
    Item works fine, but it is visual. I don't need visual.



  • A QtObject does not have a children property, which is why it cannot have nested elements. I guess you could create an element, e.g. MyQtObject, that is a QtObject with a children default property, just like Item, but with a list of MyQtObject instead of a list of Item. And then create your element using MyQtObject.



  • [quote author="ludde" date="1337760916"]A QtObject does not have a children property, which is why it cannot have nested elements.[/quote]
    Thank you, ludde! Finally I've understood about default children property.
    I changed my code to the following:
    @
    QtObject {
    property int aProperty: 5
    onAPropertyChanged: {
    someCode();
    }
    property variant nested: NestedObject {
    }
    }
    @

    It works!



  • Not exactly what I suggested, but that should work too, I guess!


Log in to reply
 

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