[Interest] QML: Considerations on an interesting design pattern that relies on JS engine's garbage collection

Ulf Hermann ulf.hermann at qt.io
Fri Apr 11 09:45:37 CEST 2025


Hi,

> ```
> Flickable {
>    id: root
>    // ...
>    property Component defaultScrollBar: Component {
>      ScrollBar { }
>    }
> 
>    ScrollBar.vertical: {
>      if (condition && root.defaultScrollBar) {
>        return root.defaultScrollBar.createObject() // a parent is not 
> passed on purpose
>      }
>      return null
>    }
> ```

This works in the sense that your ScrollBar object is garbage-collected 
when overridden. This is because the ScrollBar attached object 
un-parents it in that case and the QML engine doesn't consider it 
undeletable. However, if you create a new Component object in every 
derived type you probably haven't saved a lot of memory (but I haven't 
measured it).

What we should actually do is expose deferred properties to QML. In C++ 
you can already have deferred properties, like the background of Label. 
The default values for those are only created if they haven't been 
overridden.

best,
Ulf


More information about the Interest mailing list