[Interest] Best software engineering practices for QML

Federico Ferri federico.ferri.it at gmail.com
Tue Sep 24 13:49:56 CEST 2024


Greetings,

during last years of development with QML I always wondered how to solve
certain software engineering problems.

First, defining private properties: I have seen sometimes doing:

Item {
    property int publicProp: 40 + state.privateProp
    QtObject {
        id: state
        property int privateProp: 2
    }
}

which seems a rather clever workaround, except for some reason it does not
work with QtObject itself (error: Cannot assign to non-existent default
property):

QtObject {
    property int publicProp: 40 + state.privateProp
    QtObject {
        id: state
        property int privateProp: 2
    }
}

it is possible to do:

QtObject {
    property int publicProp: 40 + _.privateProp
    property QtObject _: QtObject {
        property int privateProp: 2
    }
}

but it exposes the private QtObject so it kind of defeats its purpose.


Then, another rather important thing is the overriding (of properties and
methods) in derived components, e.g.:

//BaseObject.qml
Item {
    readonly property bool canFly: false

    function foo() { console.log('foo()') }
}

//ExtendedObject.qml
BaseObject {
    canFly: true // Invalid property assignment: "canFly" is a read-only
property

    function foo() { console.log('foo() overridden') }
}

the property-overriding pattern would work only if the property is
writable, which is not intended.

the method-overriding pattern doesn't give any error and seems to work also
when casting an instance x of ExtendedObject with 'as', e.g.: (x as
BaseObject).foo() so I wonder if this is legal and it would be the
preferred way for overriding things (behavior) in derived components.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240924/4703b251/attachment.htm>


More information about the Interest mailing list